On 30.07.2009 16:21, Brian Candler wrote:
> Ch Ba wrote:
>> The previous returns a nomethoderror
>> for "include" in the instance of test.
> 
> Use "extend" rather than "include" if you want to add a module to a 
> single object, rather than to a class.
> 
> I think this does what you want:
> 
> module Thing
>   def self.extended(obj)
>     obj.instance_eval do
>       @bar = 5
>     end
>   end
> end

That's a nice idea to use #extended!

> class Foo
>   def initialize
>     @bar = 0
>     @foo = 2
>   end
> end
> 
> class FooBar < Foo
>   def initialize(*modules)
>     super()
>     @bar = 1
>     modules.each do |mod|
>       extend mod
>     end
>   end
> end
> 
> test = FooBar.new(Thing)
> p test
> 
> If you want to pass a symbol like :thing instead of the actual module 
> Thing, then you can use Object.const_get as others have pointed out. Or 
> build a hash:
> 
>   ALLOWED_MODULES = {
>     :thing => Thing,
>   }
> 
>   ...
>     extend ALLOWED_MODULES[mod]

May I suggest to use #fetch in that case - the error might be a bit more 
telling.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/