Can anyone tell me about the difference between class variables and class instance variables?
Can anyone tell me about the difference between class variables and class instance variables?
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
end
end
a = Foo.new
a.set_name
p a.get_name # => Nik
b = Foo.new
p 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
end
end
a = Foo.new
a.set_name
p a.get_name # => Nik
b = Foo.new
p b.get_name # => nil