Project

General

Profile

Bug #10488

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

Currently, if for some module `mod` and constant `Const`, 
 `mod.const_defined?(:Const)` is true does not imply `mod::Const` is not an error. 

 This is inconsistent for at least the following cases: 

 * if mod is a Module but not a class, `const_defined?` const_defined? will look in `Object` Object and its ancestors, but constant access (::) will not look in `Object` Object or above. 

     ~~~ruby 
     Enumerable.const_defined? :String 
     Enumerable::String #=> NameError: uninitialized constant Enumerable::String 

 * if `Const` Const is private, `const_defined?` const_defined? will return true while `mod::Const` mod::Const will raise an error. 

     ~~~ruby 
     C = 42 
     Object.private_constant :C 
     String.const_defined? :C #=> true 
     String::C #=> NameError: private constant String::C referenced 

     # This works, but is due to the lexical scope lookup 
     class String 
       C #=> 42 
     end 

 Is this intended? 
 Should it not mirror the behavior of `defined?(mod::Const)`? defined?(mod::Const)? 
 Or the behavior of `method_defined?` 
 method_defined?

Back