In Ruby, you cannot directly access class variables (i.e.
@@variable) from outside the class:
class Foo @@bar = "foobar" end # undefined method `bar' for Foo:Class (NoMethodError) puts Foo.bar
To access a class variable, you must create a class method that returns the value of the class variable:
class Foo @@bar = "foobar" def self.bar @@bar end end puts Foo.bar #=> "foobar"
It is also possible to access a class variable from a class instance by creating a getter instance method.
Hope you found this post useful. It was published . Please show your love and support by sharing this post.