class Foo @@bar = "foobar" end # undefined method `bar=' for Foo:Class (NoMethodError) Foo.bar = "foo"
class Foo @@bar = "foobar" # setter def self.bar=(val) @@bar = val end # getter def self.bar @@bar end end puts Foo.bar #=> "foobar" Foo.bar = "foo" puts Foo.bar #=> "foo"
It is also possible to set a class variable via a class instance by creating a setter instance method.
Hope you found this post useful. It was published . Please show your love and support by sharing this post.