In Ruby, reading and writing to the variables @instance(and @@classvariables) of an object must be done using the method on that object. For instance:
class TestClass
@@variable = "var"
def self.variable
@@variable
end
end
p TestClass.variable
Ruby has built-in methods for creating simple access methods. If you use a class instance variable (instead of a class variable):
class TestClass
@variable = "var"
class << self
attr_accessor :variable
end
end
Ruby on Rails offers a convenience method specifically for class variables:
class TestClass
mattr_accessor :variable
end
source
share