Jeremy Evans wrote:
> 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

There still seems to be something unique in the relationship between 
Kernel and Object. Can you replicate it?


   module MyKernel
     FOO = 42
   end

   module Kernel
     FOO = 42
   end

   class MyObject
     include MyKernel
     extend MyKernel
     # what else do we have to do?
   end

   # Alternatives to assigning to FOO above
   MyKernel.const_set(:BAR, 42)
   Kernel.const_set(:BAR, 42)

   p MyObject.const_defined?(:FOO) # ==> false
   p MyObject.const_defined?(:BAR) # ==> false

   p Object.const_defined?(:FOO)   # ==> true
   p Object.const_defined?(:BAR)   # ==> true


Can you make the outputs be true in the MyObject case?

The following works for constants that are already defined _only_ (which 
is _not_ how Object and Kernel behave):

   class MyObject
     MyKernel.constants.each do |c|
       const_set(c, MyKernel.const_get(c))
     end
   end

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407