I can't understand why accessing module's class variable fails in the following example:
module M
@@xyz = 123
end
M.class_variables # [:@@xyz]
M.class_variable_get :@@xyz # 123 , so far so good
class C
extend M
end
C.singleton_class.class_variables # [:@@xyz]
C.singleton_class.class_variable_get :@@xyz # NameError:
# uninitialized class variable @@xyz in Class
Can anybody explain why the class variable @@xyz
is suddenly inacessible/undefined in C
's singleton class?
Update: I re-tested the above code with different Ruby YARV versions and find it as a regression in the latest.
Update 2:
There was a change in definition of Module#class_variables
method in latest Ruby generation.
Ruby up to 1.9.3 the definition is
class_variables → array
Returns an array of the names of class variables in mod.
Ruby 2.0.0 latest stable version
class_variables(inherit=true) → array
Returns an array of the names of class variables in mod. This includes the names of class variables in any included modules, unless the inherit parameter is set to false.
So in latest Ruby incarnation, class_variables
returns by default also class variables of included modules. Just curious what's this feature for or if it still does concern modules "included" with include
and not extend
.
Can anybody explain ?