This part works:
class Example1
@@var1= "var1 in the Example1"
def get_var1
@@var1
end
end
example1 = Example1.new
example1.get_var1
# => "var1 in the Example1"
but if I try eigenclass:
def example1.get_var1
@@var1
end
example1.get_var1
# NameError: uninitialized class variable @@var1 in Object
# from (pry):128:in `get_var1'
Ruby looks @@var1
in the Object
instead of the Example
.
I have tested this code in the Ruby 1.9.3 and 2.0 with the same result.
Why does it happening?
The second thing, can we turn it off (so example.get_var1
won't look for class variables in the Object)?
singleton
is used for Singleton pattern. So, I don't see problems here. – CatchpennyObject#singleton_class
was added. It pains me greatly how many people I see still usingdef metaclass; class << self; self; end; end
when we've had a proper name and method for it for so long. – Phocine