On Dec 29, 3:29=A0am, Robert Klemme <shortcut... / googlemail.com> wrote:
> 2009/12/29 Intransition <transf... / gmail.com>:
>
>
>
> > 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.
>
> > =A0module M
> > =A0 =A0def m1; "m1"; end
> > =A0 =A0module N
> > =A0 =A0 =A0def self.n1; "n1"; end
> > =A0 =A0end
> > =A0end
>
> > =A0class X
> > =A0 =A0def initialize(*mods)
> > =A0 =A0 =A0(class << self; self; end).class_eval do
> > =A0 =A0 =A0 =A0include *mods
> > =A0 =A0 =A0end
> > =A0 =A0end
> > =A0 =A0def m ; m1 ; end
> > =A0 =A0def n ; N.n1 ; end
>
> Hm, this method above won't work as scope resolution rules for
> constants are different.
>
> > =A0end
>
> > =A0x =3D X.new(M)
> > =A0p x.m
> > =A0p x.n =A0 =A0#=3D> uninitialized constant X::N (NameError)
>
> > Is there any way to achieve this?
>
> Did you consider using #extend?
>
> =A0class X
> =A0 =A0def initialize(*mods)
> =A0 =A0 =A0extend *mods
> =A0 =A0end
> end
>
> What do you really want to achieve? =A0Do you have a more telling example=
?

It's for encapsulating test cases. Eg.

  TestCase.new(SomeClass) do
    ...
  end

Within the test case block it would help to handle #include, to make
tests less verbose.

Instead of making an instance of TestCase for each case, at this point
it looks like I'll have to create a new subclass of it.