Pit Capitain <pit / capitain.de> wrote in message news:<418E0C85.3050502 / capitain.de>...
> Ben schrieb:
> > I've been trying to write a method that can dynamically mixin a module
> > to a class. I tried the following code:
>  > (... code using eval ...)
> 
> Hi Ben,
> 
> in order to avoid eval as Guy suggested, you could try
> 
>    module MixinUtils
>      def self.includeMixin(klass, mixin)
>        klass.send(:include, mixin)  # include is a private method
>      end
>    end
> 
>    module Foo
>      def printFoo
>        puts "foo"
>      end
>    end
> 
>    class Array
>      def someMethod
>        self.each do |item|
>          MixinUtils.includeMixin(item.class, Foo)
>          item.printFoo
>        end
>      end
>    end
> 
> Calling
> 
>    [1,nil,true].someMethod
> 
> results in
> 
>    foo
>    foo
>    foo
> 
> as you expected.
> 
> Regards,
> Pit

I'll try this out. Thanks!