Paul Brannan wrote:
> This seems peculiar to me:
> 
> cout@bean:~/tmp$ ruby -e 'module M; FOO = 42; end; class Foo; include M; 
> end; p Foo.const_defined?(:FOO)'
> false
> cout@bean:~/tmp$ ruby -e 'Kernel.const_set(:FOO, 42); p 
> Object.const_defined?(:FOO)'
> true
> cout@bean:~/tmp$ ruby -e 'Kernel.const_set(:FOO, 42); p 
> Class.const_defined?(:FOO)'
> false
> 
> Is there a reason for this behavior?

Yes.  Namespaces:

irb(main):001:0> module M
irb(main):002:1>  FOO = 42
irb(main):003:1> end
=> 42
irb(main):004:0> class Foo
irb(main):005:1>  include M
irb(main):006:1> end
=> Foo
irb(main):007:0> Foo.const_defined?(:FOO)    # looks for Foo:FOO
=> false
irb(main):008:0> Foo::M.const_defined?(:FOO) # looks for Foo::M::FOO
(irb):8: warning: toplevel constant M referenced by Foo::M
=> true
irb(main):009:0> Object.const_defined?(:FOO) # looks for FOO
=> false
irb(main):010:0> Kernel.const_set(:FOO, 42)  # sets FOO
=> 42
irb(main):011:0> Object.const_defined?(:FOO) # looks for FOO
=> true
irb(main):012:0> Class.const_defined?(:FOO)  # looks for Class::FOO
=> false
irb(main):013:0> Class.const_set(:FOO, 42)   # sets Class::FOO
=> 42
irb(main):014:0> Class.const_defined?(:FOO)  # looks for Class::FOO
=> true

Jeremy
-- 
Posted via http://www.ruby-forum.com/.