Bug #7475

Unexpected behavior of Module#append_features on singleton class

Added by Ernie Miller over 1 year ago. Updated about 1 year ago.

[ruby-core:50356]
Status:Open
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:-
Target version:next minor
ruby -v:ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin12.2.0] Backport:

Description

=begin
A more involved explanation is available at ((URL:http://erniemiller.org/2012/11/29/ruby-tidbit-include-vs-extend-with-module-class-variables/))

In short, the handling of class variables (and constants) when a module is extended vs included is not as expected.

Example:

module Foo
  @@foo = 'foo'
end

class Bar
  include Foo
end

class Baz
  extend Foo
end

Bar.class_variable_get :@@foo # => "foo"
Baz.singleton_class.class_variable_get :@@foo # => NameError: uninitialized class variable @@foo in Class

We would expect constant and class variable lookup on the singleton class to work, but it doesn't. Both Rubinius and JRuby seem to behave as expected in this case.

Thanks!
=end

History

#1 Updated by Ernie Miller over 1 year ago

Another quick set of observations from this morning:

class Baz
  class << self
    Const = 'Const'
    @@foo = 'foo'
  end
end

Baz.class_variables.inspect # => [:@@foo]
Baz.singleton_class.class_variables.inspect # => []
Baz.singleton_class.class_variable_get :@@foo # => 'foo' ???
Baz.const_get(:Const, false) rescue "Nope." # => 'Nope.'
Baz.singleton_class.const_get(:Const, false) rescue "Nope." # => Const

# Let's try setting it explicitly.
Baz.singleton_class.class_variable_set :@@foo, 'foo'
Baz.singleton_class.class_variables.inspect # => [] -- still "empty"

However, if we extend Foo on Baz, vs opening the singleton class with "class << self", we can classvariableset on the singleton and see it show up in the list of class variables, vs being empty.

All of this is to say that I think some unexpected weirdness is going on in rbincludemodule and/or includeclassnew.

#2 Updated by Koichi Sasada about 1 year ago

  • Assignee set to Yukihiro Matsumoto
  • Target version changed from 1.9.3 to next minor

Matz, could you check it?

#3 Updated by Nobuyoshi Nakada about 1 year ago

  • Description updated (diff)

Also available in: Atom PDF