(Question already posted at Ruby Forum, but did not evoke any answer there).
This is my code:
class MC
def initialize
@x = 5
@@y = 6
end
def f
puts @x
puts @@y
end
end
m = MC.new
m.f
m.f produces the expected output without an error:
5
6
But this:
def m.g
puts @x
puts @@y
end
m.g
produces:
5
warning: class variable access from toplevel
NameError: uninitialized class variable @@y in Object
Why can I access @@y from f, but not from g?
Mentioning of toplevel and Object in the warning and the error message is puzzling to me.
@x is printed as 5, so its environment is MC. This excludes the possibility that @x and @@y in the definition of m.g refer to the toplevel environment (Object) instead of MC.
Why did I get the error message?