Saturday, December 27, 2008

Class and class variables...

When is a class variable a class variable? When it's not an instance variable I guess:
irb>>
>> class Foo
>> attr_reader :foo
?> @foo = "I exist in the class object" #class instance variable
>> @@foo = "I have no idea where I exist" #class variable
>>
?> def initialize(*args)
>> @foo = "I am an instance variable" #instance variable
>> end
>>
?> def self.read_foo
>> puts Foo.new.foo
>> puts @foo
>> puts @@foo
>> end
>> end
=> nil
>>
?> Foo.read_foo
I am an instance variable
I exist in the class object
I have no idea where I exist
=> nil
>>

No comments: