On Wed, 7 Feb 2007, Jeff Barczewski wrote:

> I am searching for the best way to do a dynamic mixin with Ruby.
>
> Basically I have a class that includes a mixin but I want the mixin module
> name to be driven from configuration and not the code.
>
> class A
> include Foo
> end
>
>
> but instead of Foo being constant module name, I would this to be driven off
> of a configuration value. (I'm not too worried about the module being loaded
> previously that is easily taken care of, so primarily just wanting to know
> the best way to do a dynamic include).
>
> Any opinions on the best way to accomplish this?
>
>
> A very simple way to approach this might be something like
>
> ModToInclude = 'Foo'

   ModToInclude = 'Foo;  system "sudo rm -rf /"'

>
> class A
> eval( "include #{ModToInclude}" )
> end

>
>
> Any better ideas?
>
   def factory plugin
     plugins = {
       :foo => Foo,
       :bar => Bar,
     }
     Class.new{
       include plugins[ plugin ]
     }
   end


   A = factory :foo


is one simple way.

regards.

-a
-- 
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama