I get a warning:
"class variable access from toplevel singleton method"
to object a, and other instances of A don't have access to that
method. The class variable @@v is also added to the same object
specific class. But then, what exactly does this warning mean, and how
bad is it?
Thanks
Raph
···
From my understanding, the test method is added to the class specific
So methods are added to the object specific class (the metaclass as
identified in why's article), but class variables are added to the
class of which the object is an instance. Why the difference?
Further on this, if I defined a second instance and try to call test:
b=A.new(6)
b.test
I get the error that test is a private method:
NoMethodError: private method `test' called for #<A:0xb7cbf5ec @value=6>
so it seems it was added to A, but marked as private.
Are there any good reading about this?
Oh, and about my initial question: how bad is the warning I get?
Raph
···
On 5/10/06, ts <decoux@moulon.inra.fr> wrote:
> method. The class variable @@v is also added to the same object
> specific class.
Are you sure ?
moulon% ruby -e 'class << Object.new; @@a = 12 end; p @@a'
-e:1: warning: class variable access from toplevel singleton method
12
So methods are added to the object specific class (the metaclass as
identified in why's article), but class variables are added to the
class of which the object is an instance.
Well, no. class variables (i.e @@) are added in the class which is
cuurently in scope.
Further on this, if I defined a second instance and try to call test:
b=A.new(6)
b.test
When you do this, ruby find the method Kernel#test which is a private
method. This is why it give a warning.
so it seems it was added to A, but marked as private.
No, it was added to the singleton class of `a', you have just chosen a bad
name for your method (test)
Oh, and about my initial question: how bad is the warning I get?
ruby is trying to say that the variable @@v will be defined in Object,
because there is no other class where it can define it.