Difference between class variables and class instance variables?
A class variable (@@
) is shared among the class and all of its descendants. A class instance variable (@
) is not shared by the class's descendants.
Class variable (@@
)
Let's have a class Foo with a class variable @@i
, and accessors for reading and writing @@i
:
class Foo @@i = 1 def self.i @@i end def self.i=(value) @@i = value endend
And a derived class:
class Bar < Fooend
We see that Foo and Bar have the same value for @@i
:
p Foo.i # => 1p Bar.i # => 1
And changing @@i
in one changes it in both:
Bar.i = 2p Foo.i # => 2p Bar.i # => 2
Class instance variable (@
)
Let's make a simple class with a class instance variable @i
and accessors for reading and writing @i
:
class Foo @i = 1 def self.i @i end def self.i=(value) @i = value endend
And a derived class:
class Bar < Fooend
We see that although Bar inherits the accessors for @i
, it does not inherit @i
itself:
p Foo.i # => 1p Bar.i # => nil
We can set Bar's @i
without affecting Foo's @i
:
Bar.i = 2p Foo.i # => 1p Bar.i # => 2
First you must understand that classes are instances too -- instances of the Class
class.
Once you understand that, you can understand that a class can have instance variables associated with it just as a regular (read: non-class) object can.
Hello = Class.new# setting an instance variable on the Hello classHello.instance_variable_set(:@var, "good morning!")# getting an instance variable on the Hello classHello.instance_variable_get(:@var) #=> "good morning!"
Note that an instance variable on Hello
is completely unrelated to and distinct from an instance variable on an instance of Hello
hello = Hello.new# setting an instance variable on an instance of Hellohello.instance_variable_set(:@var, :"bad evening!")# getting an instance variable on an instance of Hellohello.instance_variable_get(:@var) #=> "bad evening!")# see that it's distinct from @var on HelloHello.instance_variable_get(:@var) #=> "good morning!"
A class variable on the other hand is a kind of combination of the above two, as it accessible on Hello
itself and its instances, as well as on subclasses of Hello
and their instances:
HelloChild = Class.new(Hello)Hello.class_variable_set(:@@class_var, "strange day!")hello = Hello.newhello_child = HelloChild.newHello.class_variable_get(:@@class_var) #=> "strange day!"HelloChild.class_variable_get(:@@class_var) #=> "strange day!"hello.singleton_class.class_variable_get(:@@class_var) #=> "strange day!"hello_child.singleton_class.class_variable_get(:@@class_Var) #=> "strange day!"
Many people say to avoid class variables
because of the strange behaviour above, and recommend the use of class instance variables
instead.
Also I want to add that you can get access to the class variable (@@
) from any instance of the class
class Foo def set_name @@name = 'Nik' end def get_name @@name endenda = Foo.newa.set_namep a.get_name # => Nikb = Foo.newp b.get_name # => Nik
But you can't do the same for the class instance variable(@
)
class Foo def set_name @name = 'Nik' end def get_name @name endenda = Foo.newa.set_namep a.get_name # => Nikb = Foo.newp b.get_name # => nil