Thursday, May 5, 2011

When do Ruby instance variables get set?

class Hello
@hello = "hello"
    def display
     puts @hello
    end
end

h = Hello.new
h.display

I created the class above. It doesn't print anything out. I thought the instance variable @hello was set during the class declaration. But when I call the display method the output is 'nil'. What's the correct way to do this?

From stackoverflow
  • You need to add an initialize method:

    class Hello
        def initialize
            @hello = "hello"
        end
        def display
            puts @hello
        end
    end
    
    h = Hello.new
    h.display
    
  • The first @hello in your code is called a class instance variable.

    It's an instance variable of the class object which the constant Hello points to. (and which is an instance of the class Class.)

    Technically, when you're within the class scope, your self is set to the object of your current class, and @variables pertain to your current self. Boy I suck at explaining these things.

    You can get all this and much more clarified to you by watching this collection of $5-each screencasts from The Pragmatic Programmers.

    (Or you can ask for clarifications here and I'll try to update.)

  • Instance variables in ruby may be a bit confusing when first learning Ruby, especially if you are accustomed to another OO language like Java.

    You cannot simply declare an instance variable.

    One of the most important things to know about instance variables in ruby, apart from the notation with an @ sign prefix, is that they spring into life the first time they are assigned to.

    class Hello
      def create_some_state
        @hello = "hello"
      end
    end
    
    h = Hello.new
    p h.instance_variables 
    
    h.create_some_state
    p h.instance_variables
    
    # Output
    []
    ["@hello"]
    

    You can use the method Object#instance_varaibles to list all instance variables of an object.

    You normally “declare” and initialize all the instance variables in the initialize method. Another way to clearly document which instance variables that should be publicly available is to use the Module methods attr_accessor (read/write), attr_writer (write) and attr_reader (read). These methods will synthesize different accessor methods for the listed instance variable.

    class Hello
      attr_accessor :hello
    end
    
    h = Hello.new
    p h.instance_variables 
    
    h.hello = "hello"
    p h.instance_variables
    
    # Output
    []
    ["@hello"]
    

    The instance variable still isn’t created until it’s assigned to using the synthesized Hello#hello= method.

    Another important issue, like kch described, is that you need to be aware of the different contexts active when declaring a class. When declaring a class the default receiver (self) in the outermost scope will be the object that represents the class itself. Hence your code will first create a class instance variable when assigning to @hello on the class level.

    Inside methods self will be the object on which the method is invoked, hence you are trying print the value of an instance variable with the name @hello in the object, which doesn’t exists (note that it’s perfectly legal to read a non existing instance variable).

0 comments:

Post a Comment