Forum: Ruby-core [ruby-trunk - Bug #7475][Open] Unexpected behavior of Module#append_features on singleton class

Posted by ernie (Ernie Miller) (Guest)
on 2012-11-30 03:03
(Received via mailing list)
Issue #7475 has been reported by ernie (Ernie Miller).

----------------------------------------
Bug #7475: Unexpected behavior of Module#append_features on singleton 
class
https://bugs.ruby-lang.org/issues/7475

Author: ernie (Ernie Miller)
Status: Open
Priority: Normal
Assignee:
Category:
Target version: 1.9.3
ruby -v: ruby 1.9.3p327 (2012-11-10 revision 37606) 
[x86_64-darwin12.2.0]


A more involved explanation is available at 
http://erniemiller.org/2012/11/29/ruby-tidbit-incl...

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!
Posted by ernie (Ernie Miller) (Guest)
on 2012-11-30 14:52
(Received via mailing list)
Issue #7475 has been updated by ernie (Ernie Miller).


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 class_variable_set 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 rb_include_module and/or include_class_new.
----------------------------------------
Bug #7475: Unexpected behavior of Module#append_features on singleton 
class
https://bugs.ruby-lang.org/issues/7475#change-34221

Author: ernie (Ernie Miller)
Status: Open
Priority: Normal
Assignee:
Category:
Target version: 1.9.3
ruby -v: ruby 1.9.3p327 (2012-11-10 revision 37606) 
[x86_64-darwin12.2.0]


A more involved explanation is available at 
http://erniemiller.org/2012/11/29/ruby-tidbit-incl...

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!
Posted by ko1 (Koichi Sasada) (Guest)
on 2013-02-18 01:07
(Received via mailing list)
Issue #7475 has been updated by ko1 (Koichi Sasada).

Assignee set to matz (Yukihiro Matsumoto)
Target version changed from 1.9.3 to next minor

Matz, could you check it?

----------------------------------------
Bug #7475: Unexpected behavior of Module#append_features on singleton 
class
https://bugs.ruby-lang.org/issues/7475#change-36458

Author: ernie (Ernie Miller)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version: next minor
ruby -v: ruby 1.9.3p327 (2012-11-10 revision 37606) 
[x86_64-darwin12.2.0]


A more involved explanation is available at 
http://erniemiller.org/2012/11/29/ruby-tidbit-incl...

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!
Posted by Nobuyoshi Nakada (nobu)
on 2013-02-18 02:55
(Received via mailing list)
Issue #7475 has been updated by nobu (Nobuyoshi Nakada).

Description updated


----------------------------------------
Bug #7475: Unexpected behavior of Module#append_features on singleton 
class
https://bugs.ruby-lang.org/issues/7475#change-36480

Author: ernie (Ernie Miller)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version: next minor
ruby -v: ruby 1.9.3p327 (2012-11-10 revision 37606) 
[x86_64-darwin12.2.0]


=begin
A more involved explanation is available at 
((<URL:http://erniemiller.org/2012/11/29/ruby-tidbit-incl...))

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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.