- Why does
Foo.valreturnnilinstead of"foo"before callingFoo.set? - Is there any mechanism for initializing
@valon class evaluation? -
In which scope is
@val = "foo"stored into?class Foo class << self @val = "foo" attr_reader :val def set(val) @val = val end end end p Foo.val # nil Foo.set("bar") p Foo.val # "bar"
Why does Foo.val return nil instead of foo before calling Foo.set ? Is there
Share
You can initialize
@valin Foo like this:Your code initializes
@valnot on Foo, but on Foo’s metaclass