instance and class variables in rails controller

后端 未结 5 1436
一整个雨季
一整个雨季 2021-01-30 23:21

I\'m new to rails and ruby. I was studying the concept of class and instance variables. I understood the difference but when I tried it out using the controller in rails it got

相关标签:
5条回答
  • 2021-01-30 23:39

    Declaring the @insworld within the class but not in the constructor or any of the instance methods sets @insworld scoped to the instance of the class itself.

    BooksController.instance_variable_get(:'@insworld')
    

    If you need the access to variable inside of your index method, consider defining it within the method instead.

    0 讨论(0)
  • 2021-01-30 23:46

    Classes are also objects in Ruby, so they can have their own instance variables which are called class instance variables.

    • @@world is a class variable
    • @insworld is a class instance variable
    • #index is an instance method

    When you try to access @insworld in #index, Ruby searches for the instance variable in the A object (meaning A.new) because #index is an instance method.

    But you defined @insworld as a class instance variable which means it is defined in the class object itself (meaning A).

    The following code demonstrates:

    class Hi
      @@a = 1 # class variable
      @b  = 2 # class instance variable
    
      def initialize
        @c = 3 # instance variable
      end
    
      def test # instance method, works on objects of class Hi
        puts @@a # => 1
        puts @b  # => nil, there is no instance variable @b
        puts @c  # => 3 # we defined this instance variable in the initializer
      end
    end
    
    Hi.class_variables        # => @@a
    Hi.instance_variables     # => @b
    Hi.new.instance_variables # => @c
    # Hi is an object of class Class
    # Hi.new is an object of class Hi
    

    Keep in mind that all instance variables return nil if they don't exist.

    0 讨论(0)
  • 2021-01-30 23:46

    When you declare instance variable outside of method, you might not get the result you want. It is an instance variable, yes, but it belongs to the class itself (which is an instance of class Class).

    class Foo
      @myvar = "class-level"
    
      def initialize
        @myvar = 'instance-level'
      end
    end
    
    f = Foo.new
    
    f.class.instance_variable_get(:@myvar) # => "class-level"
    f.instance_variable_get(:@myvar) # => "instance-level"
    f.instance_variable_get(:@myvar2) # => nil 
    

    If you try to get value of uninitialized ivar, it will evaluate to nil. That's why you got nil in your experiments: the variable doesn't exist at that scope.

    To get instance-level variables, set them in methods/actions.

    0 讨论(0)
  • 2021-01-30 23:46

    The scope of Instance variables are from action to view they initiate when any action occurs and destroy when action ends.

    0 讨论(0)
  • 2021-01-30 23:51

    When you declare @instworld you are inside BooksController class (i.e. self will return BooksController. Weird thing in ruby is that classes are also objects (the are instances of class Class) hence you in fact declares instance variable @instworld for this particular instance of class Classm not for instance of BooksController.

    You can check it really easily by declaring class method:

    class A
      # self here returns class A
      @variable = 'class instance variable'
      @@variable = 'class variable'
    
      def initalize
        # self here returns the instance
        @variable = 'instance variable'
      end
    
      def self.test_me
        # self here returns class A
        @variable
      end
    
      def test_me
        # self returns the instance
        @variable
      end
    
      #class variable is accessible by both class and instance 
      def test_me2
        @@variable
      end
    
      def self.test_me2
        @@variable
      end
    end
    
    A.test_me       #=> 'class instance variable'
    A.new.test_me   #=> 'instance variable'
    A.test_me2      #=> 'class variable'
    A.new.test_me2  #=> 'class variable'
    
    0 讨论(0)
提交回复
热议问题