Bug #8297

extend & inherited class variable issue

Added by David Unric 12 months ago. Updated 12 months ago.

[ruby-core:54463]
Status:Open
Priority:Normal
Assignee:-
Category:core
Target version:-
ruby -v:2.0.0p0 Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN

Description

=begin
By the current documentation, (({Object#extend})) method has to (only) add instance methods of a module given as a parameter.
In the following example, the class ((|C|)) is extended with module ((|M|)).
By (({class_variables})) method sent to singleton class of C also did inherit class variable ((|@@xyz|)).
However when inherited ((|@@xyz|)) is accessed, (({NameError})) exception is raised as it is was not initialized.

module M
@@xyz = 123
end
M.classvariables # [:@@xyz]
M.class
variable_get :@@xyz # 123 , so far so good

class C
extend M
end
p C.singletonclass.classvariables # [:@@xyz]
p C.singletonclass.classvariable_get :@@xyz # NameError exception

Either (({classvariables})) returns invalid array - ie. ((|@@xyz|)) was not inherited at all or (({classvariableget})) ignores class variables inherited from module (when sent to a singleton).
=end
Prior Ruby versions like 1.9.3p392 does not suffer this issue as return with Module#class
variables returns an empty array.

History

#1 Updated by David Unric 12 months ago

=begin
By the current documentation, Object#extend method has to (only) add instance methods of a module given as a parameter.
In the following example, the class ((C)) is extended with module ((M)).
By ((class_variables)) method sent to singleton class of C also did inherit class variable ((@@xyz)).
However when inherited ((@@xyz)) is accessed, ((NameError)) exception is raised as it is was not initialized:

module M
@@xyz = 123
end

M.classvariables # [:@@xyz]
M.class
variable_get :@@xyz # 123 , so far so good

class C
extend M
end
p C.singletonclass.classvariables # [:@@xyz]
p C.singletonclass.classvariable_get :@@xyz # NameError exception

Either ((class_variables)) returns invalid array - ie. ((@@xyz)) was not inherited at all or ((classvariableget)) ignores class variables inherited from module (when sent to a singleton).

Prior Ruby versions like 1.9.3p392 does not suffer this issue as Module#class_variables returns an empty array.
=end

#2 Updated by Nobuyoshi Nakada 12 months ago

  • Description updated (diff)

Also available in: Atom PDF