I studied this question a bit, and I think it is caused by an MRI error / inconsistency / idiosyncrasy.
In MRI 2.1.0 this code is:
class MyClass class << self MC_CONST = 30 end end p MyClass.singleton_class.const_defined? :ABBA_CONST, false p MyClass.singleton_class.const_defined? :MC_CONST, false p MyClass.singleton_class.constants(false)
gives
false true []
Thus, the MC_CONST constant MC_CONST defined, but it is not available as a local class constant (I pass false values ββto various methods to disable constant resolution and just keep it local for this class), which should be true. If we check the documentation of the constants of module #, then it says:
Returns an array of constant names available in the module. This includes the names of the constants in any included modules (an example at the beginning of the section) if all is not set to false.
Also see module :: const_defined? .
So what do we need to check const_defined? to better understand the behavior of constants , but these two methods give different results!
In addition, in another Ruby implementation, this code works as expected.
In JRuby 1.7.9 it gives:
false true [:MC_CONST]
In Rubinius 2.2.1, it gives:
false true [:MC_CONST]
What is the expected behavior :)
Ju liu
source share