Bug #9413

Object Metaclass Constants Array doesn't Include Singleton Constant

Added by Dmitri Moore about 1 year ago. Updated about 1 year ago.

[ruby-core:<unknown>]
Status:Closed
Priority:Normal
Assignee:-
ruby -v:ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0] Backport:1.9.3: DONE, 2.0.0: DONE, 2.1: DONE

Description

Let's assume we have this Ruby class:

class MyClass
  class << self
    MC_CONST = 30
  end
end 

Then, let's instantiate MyClass and add another constant to the object's metaclass:

m = MyClass.new
class << m
  OBJ_MC_CONST = 50
end

There are no issues with the object singleton constant:

m.singleton_class::OBJ_MC_CONST # => 50 <-- [OK]
m.singleton_class.constants.include? :OBJ_MC_CONST # => true <- [OK]

But not exactly what I'd expect with the class singleton constant:

MyClass.singleton_class::MC_CONST # => 30 <-- [OK]
MyClass.singleton_class.const_get :MC_CONST # => 30 <-- [OK]
MyClass.singleton_class.const_defined? :MC_CONST=> true <-- [OK]
MyClass.singleton_class.constants.include? :MC_CONST # => false <-- [Why???]

Why does the array returned by .constants method on metaclass of MyClass class doesn't contain :MC_CONST?

Here is the SO (StackOverflow) thread for this http://stackoverflow.com/questions/21057002/where-are-the-ruby-class-singleton-constants-stored/21061117?iemail=1&noredirect=1#21061117

Associated revisions

Revision 44628
Added by Nobuyoshi Nakada about 1 year ago

eval.c: singleton class constants

  • eval.c (rb_mod_s_constants): return its own constants for other than Module itself. [Bug #9413]

Revision 44628
Added by Nobuyoshi Nakada about 1 year ago

eval.c: singleton class constants

  • eval.c (rb_mod_s_constants): return its own constants for other than Module itself. [Bug #9413]

History

#1 Updated by Nobuyoshi Nakada about 1 year ago

  • Backport changed from 1.9.3: UNKNOWN, 2.0.0: UNKNOWN, 2.1: UNKNOWN to 1.9.3: REQUIRED, 2.0.0: REQUIRED, 2.1: REQUIRED

#2 Updated by Nobuyoshi Nakada about 1 year ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

Applied in changeset r44628.


eval.c: singleton class constants

  • eval.c (rb_mod_s_constants): return its own constants for other than Module itself. [Bug #9413]

#3 Updated by Usaku NAKAMURA about 1 year ago

  • Backport changed from 1.9.3: REQUIRED, 2.0.0: REQUIRED, 2.1: REQUIRED to 1.9.3: DONE, 2.0.0: REQUIRED, 2.1: REQUIRED

backported into ruby_1_9_3 at r44939.

#4 Updated by Tomoyuki Chikanaga about 1 year ago

  • Backport changed from 1.9.3: DONE, 2.0.0: REQUIRED, 2.1: REQUIRED to 1.9.3: DONE, 2.0.0: DONE, 2.1: REQUIRED

r44628 was backported to ruby_2_0_0 at r45010.

#5 Updated by Yui NARUSE about 1 year ago

  • Backport changed from 1.9.3: DONE, 2.0.0: DONE, 2.1: REQUIRED to 1.9.3: DONE, 2.0.0: DONE, 2.1: DONE

ruby_2_1 r45124 merged revision(s) 44628.

Also available in: Atom PDF