S
Sam Kong
Hello!
I have a question about class variable.
class C1
@@a = "C1"
def f
@@a
end
end
class C2 < C1
@@a = "C2"
end
puts C2.new.f #=> "C2"
It's understandable.
When I called C2.new.f
the method f seems to find C2's @@a.
Now I removed @@a from C1.
class C1
def f
@@a
end
end
class C2 < C1
@@a = "C2"
end
puts C2.new.f
#Result: in `f': uninitialized class variable @@a in C1 (NameError)
Why does it bother with C1's @@a if it returns C2's @@a?
I assume that the method f should behave polymorphically and the body
of f should be interpreted in the context of C2's instance method if I
call it via an instance of C2.
Probably I misunderstand something.
Can anybody enlighten me on this?
Thanks in advance.
Sam
I have a question about class variable.
class C1
@@a = "C1"
def f
@@a
end
end
class C2 < C1
@@a = "C2"
end
puts C2.new.f #=> "C2"
It's understandable.
When I called C2.new.f
the method f seems to find C2's @@a.
Now I removed @@a from C1.
class C1
def f
@@a
end
end
class C2 < C1
@@a = "C2"
end
puts C2.new.f
#Result: in `f': uninitialized class variable @@a in C1 (NameError)
Why does it bother with C1's @@a if it returns C2's @@a?
I assume that the method f should behave polymorphically and the body
of f should be interpreted in the context of C2's instance method if I
call it via an instance of C2.
Probably I misunderstand something.
Can anybody enlighten me on this?
Thanks in advance.
Sam