# Dynamic Modules

**URL:** <https://www.ruby-forum.com/t/dynamic-modules/49316>\
**Category:** Ruby\
**Created:** [December 2, 2005, 2:58pm UTC](https://www.ruby-forum.com/t/dynamic-modules/49316 "2005-12-02T14:58:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![transfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@transfire](https://www.ruby-forum.com/u/transfire)\
**Post date:** [December 2, 2005, 2:58pm UTC](https://www.ruby-forum.com/t/dynamic-modules/49316/1 "2005-12-02T14:58:30Z")

</div>

I would like to discuss the merits/non-merits and the best approach to  
implementation of “dynamic modules” --modules that take parameters at  
the time of include, thus altering their behavior per include. The  
common denominator here is the altered interface of #include:

include AModule, :opt1 =\> :val1, :opt2 =\> :val2

Here’s a simple example based on the implementation I (and friends) put  
together

module Mixin

```
 dynamic_feature do |options|

     define_method :hello do
       puts "Hello from #{options[:name]}"
     end

 end

```

end

class MyClass  
include Mixin, :name =\> ‘Ruby’  
end

m = MyClass.new  
m.hello -\> ‘Hello from Ruby’

class MyClass2  
include Mixin, :name =\> ‘Tom’  
end

m = MyClass2.new  
m.hello -\> ‘Hello from Tom’

T.

---

<div class="post-metadata">

**Author:** ![transfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@transfire](https://www.ruby-forum.com/u/transfire)\
**Post date:** [December 2, 2005, 3:47pm UTC](https://www.ruby-forum.com/t/dynamic-modules/49316/2 "2005-12-02T15:47:22Z")

</div>

Hi,

Why don’t you just create a factory method with the same name as your  
class which creates an altered version of your base module? So your  
include could be something like the following:

module MyMixin  
…  
end

def MyMixIn(option1, option2)  
MyMixin # but then modified ofcourse…  
end

class A  
include MyMixin(option1, option2)  
end

Regards,

Peter

---

<div class="post-metadata">

**Author:** ![transfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@transfire](https://www.ruby-forum.com/u/transfire)\
**Post date:** [December 2, 2005, 4:07pm UTC](https://www.ruby-forum.com/t/dynamic-modules/49316/3 "2005-12-02T16:07:44Z")

</div>

Trans wrote:

> module Mixin
> 
> m = MyClass2.new  
> m.hello -\> ‘Hello from Tom’
> 
> T.

I’d rather see this:

class Module  
def include(mod, \*args, &block)  
mod.send :append\_features, self  
if mod.respond\_to? :included  
mod.included(self, \*args, &block)  
end  
end  
end

module Chewable  
def self.included(klass, \*args)  
puts “Included in #{klass} with the following args:”  
args.each { |arg| puts " \* #{arg.inspect}" }  
end  
end

class Klass  
include Chewable “foo”, “bar”, “baz”  
end

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![transfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@transfire](https://www.ruby-forum.com/u/transfire)\
**Post date:** [December 2, 2005, 4:07pm UTC](https://www.ruby-forum.com/t/dynamic-modules/49316/4 "2005-12-02T16:07:45Z")

</div>

Daniel S. wrote:

> > end  
> > end  
> > def include(mod, \*args, &block)  
> > args.each { |arg| puts " \* #{arg.inspect}" }  
> > end  
> > end
> 
> class Klass  
> include Chewable “foo”, “bar”, “baz”  
> end
> 
> Cheers,  
> Daniel

That should of course be `include Chewable, “foo”…’

---

<div class="post-metadata">

**Author:** ![transfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@transfire](https://www.ruby-forum.com/u/transfire)\
**Post date:** [December 2, 2005, 5:49pm UTC](https://www.ruby-forum.com/t/dynamic-modules/49316/5 "2005-12-02T17:49:18Z")

</div>

Daniel S. wrote:

> > > together
> > > 
> > > ```
> > > include Mixin, :name => 'Tom'
> > > 
> > > ```
> > 
> > class Module  
> > puts “Included in #{klass} with the following args:”  
> > Daniel
> 
> That should of course be `include Chewable, “foo”…’

Current implementation:

class Module  
def include(mod, \*args, &block)  
mod.send :append\_features, self  
if mod.respond\_to? :included  
mod.included(self, \*args, &block)  
end  
end  
end

class Class  
def is(\*mods)  
mods.each { |mod| include mod }  
end  
end

class Klass  
is Chewable, Digestible  
include MyModule, “foo”, “bar”, “baz”  
end

---

<div class="post-metadata">

**Author:** ![transfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@transfire](https://www.ruby-forum.com/u/transfire)\
**Post date:** [December 2, 2005, 6:58pm UTC](https://www.ruby-forum.com/t/dynamic-modules/49316/6 "2005-12-02T18:58:24Z")

</div>

Also, can anyone confirm this? I heard someone say that  
#append\_features is being deprecated. True?

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![transfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@transfire](https://www.ruby-forum.com/u/transfire)\
**Post date:** [December 2, 2005, 6:50pm UTC](https://www.ruby-forum.com/t/dynamic-modules/49316/7 "2005-12-02T18:50:17Z")

</div>

Hi Peter,

A factory method is a fair idea, but there’s a lot more to this:

MyMixin # but then modified ofcourse…

then that simple comment suggests. How do you handle _that_? That’s  
really the heart of the matter --making a factory or overriding  
#include to pass options is the easy part.

Thanks,  
T.
