From: ara.t.howard@... Date: 2006-09-21T02:19:23+09:00 Subject: Re: const_defined? not quite in sync with const_get ?? On Thu, 21 Sep 2006, Chris Roos wrote: > Assuming the following snippet, I'd expect Foo::const_defined?(:Bar) > to be routed via Foo::const_missing. Instead it resolves to top-level > Bar. Can someone explain why please? > > --code-- > > class Bar > end > > module Foo > def self.const_missing(konst) > puts "missing konst #{konst}" > end > end > > Foo::Bar > #=> missing konst Bar # Unsurprising behaviour > > p Foo::const_defined?(:Bar) > false # Unsurprising behaviour > > p Foo::const_get(:Bar) > #=> Bar # Surprising behaviour > > --/code-- > > Cheers, > > Chris non-prefixed constants follow the normal scoping rules: harp:~ > cat a.rb class Bar end module Foo def self.const_missing(konst) puts "missing konst #{konst}" end end Foo::Bar #=> missing konst Bar # Unsurprising behaviour p Foo::const_defined?('Bar') false # Unsurprising behaviour p Foo::const_get('Foo::Bar') # ^^^^^ # ^^^^^ # ^^^^^ #=> Bar # Surprising behaviour harp:~ > ruby a.rb missing konst Bar false a.rb:16:in `const_get': wrong constant name Foo::Bar (NameError) from a.rb:16 -a -- in order to be effective truth must penetrate like an arrow - and that is likely to hurt. -- wei wu wei