On Dec 28, 2009, at 10:29 PM, Intransition wrote:

> I would like to use #include at an instance level, such that it
> behaves just as it does at a class level. After a number of
> experiments I thought for sure it would work if I ran the include
> through the object's singleton. Alas, submodules remain inaccessible,
> eg.
>=20
>  module M
>    def m1; "m1"; end
>    module N
>      def self.n1; "n1"; end
>    end
>  end
>=20
>  class X
>    def initialize(*mods)
>      (class << self; self; end).class_eval do
>        include *mods
>      end
>    end
>    def m ; m1 ; end
>    def n ; N.n1 ; end
>  end
>=20
>  x =3D X.new(M)
>  p x.m
>  p x.n    #=3D> uninitialized constant X::N (NameError)
>=20
> Is there any way to achieve this?
>=20
You may write:

 class X
   def eigenclass
     class << self; self; end;
   end
  =20
   def initialize(*mods)
     eigenclass.instance_eval do
       include *mods=20
     end
   end
   def m ; m1 ; end
   def n;  eigenclass::N.n1 ; end
 end

I guess constants not like methods are not searched within eigenclass, =
and that's the reason why you are getting an error.
--
Fedor Kocherga
http://sidenotes.kocherga.info/