David A. Black ha scritto:
> Hi --
> 
> On Sun, 14 Aug 2005, gabriele renzi wrote:
> 
>> Hi gurus and nubys,
>>
>> is there a way to add a constant into an object which is not a 
>> module/class?
>>
>> Basically what I'd like is some kind of append_features/include that 
>> works on non-module objects.
>>
>> Is my only chance to mess up with the singleton class by myself, or 
>> there is some existing method for this?
> 
> 
> If I understand you correctly, I think Kernel#extend is what you need:
> 
>   irb(main):001:0> s = ""
>   => ""
>   irb(main):002:0> module M; X=1; end
>   => 1
>   irb(main):003:0> s.extend(M)
>   => ""
>   irb(main):004:0> (class << s; self; end)::X
>   => 1
> 
> I think that's the closest thing corresponding to "adding a constant"
> to an arbitrary object (adding it to the object's singleton class).

well, ri explicitly says for #extend:
" Adds to _obj_ the instance methods from each module given as a
  parameter"
and this is omitting Constants, which are available when something is 
#include'd
See example:

I won't be able to access singleton class constants from the singleton 
instance, it seems:

 >> class Foo
 >>  def barer
 >>   Bar.new
 >>  end
 >> end
=> nil
 >> module M
 >>  class Bar
 >>  end
 >> end
=> nil
 >> foo=Foo.new
=> #<Foo:0x2d01428>
 >> foo.extend M
=> #<Foo:0x2d01428>
 >> foo.barer   # I'd like to get a Bar instance here
NameError: uninitialized constant Foo2::Bar
         from (irb):12:in `barer'
         from (irb):21

I don't understand why, actually.