# Include module in module

**URL:** <https://www.ruby-forum.com/t/include-module-in-module/155107>\
**Category:** Ruby\
**Created:** [December 8, 2008, 8:13am UTC](https://www.ruby-forum.com/t/include-module-in-module/155107 "2008-12-08T08:13:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [December 8, 2008, 8:13am UTC](https://www.ruby-forum.com/t/include-module-in-module/155107/1 "2008-12-08T08:13:55Z")

</div>

Hi,

I have a situation like this:

module M

…

end

class C1

include M

…

end

class C2

include M

…

end

Now I want to add methods to C1 and C2 and whatever other class includes  
M.

My first approach was:

module S

…

end

module M

include S

end

But this doesn’t work (meaning whatever methods I have in S are  
undefined for instances of C1, C2).

So, how can I do that?

Thanks,

Ittay

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [December 8, 2008, 8:55am UTC](https://www.ruby-forum.com/t/include-module-in-module/155107/2 "2008-12-08T08:55:31Z")

</div>

Ittay D. wrote:

> class C2
> 
> module M  
> So, how can I do that?  
> I think I solved it:  
> module S  
> def self.included(other)  
> methods = instance\_methods(false)  
> other.module\_eval do  
> methods.each do |name|  
> name = name.to\_sym  
> base.send :define\_method, name, instance\_method(name)  
> end  
> end  
> end  
> end

Ittay

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [December 8, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/include-module-in-module/155107/3 "2008-12-08T13:10:35Z")

</div>

2008/12/8 Ittay D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> include M  
> end
> 
> Now I want to add methods to C1 and C2 and whatever other class includes M.

> So, how can I do that?

There are at least these two possible approaches

1. add methods to M
2. create another module S and include it in all classes inheriting M  
2b. same as 2 but insert S only in the inheritance hierarchy of  
classes that directly include M

#!/bin/env ruby

module M  
def m  
“Mmmmh!”  
end  
end

class A  
include M  
end

class B  
include M  
end

# approach 1

module M  
def late  
“late”  
end  
end

puts A.new.late, B.new.late

# approach 2

module S  
def even\_later  
“even\_later”  
end  
end

ObjectSpace.each\_object(Class) do |cl|  
if cl.ancestors.include?(M) && !cl.ancestors.include?(S)  
cl.class\_eval { include S }  
end  
end

puts A.new.even\_later, B.new.even\_later

# approach 2b

module S2  
def x  
“X”  
end  
end

ObjectSpace.each\_object(Class) do |cl|  
a = cl.ancestors  
i = a.index(M)  
if i && a[1…i].all? {|c| not Class === c}  
cl.class\_eval { include S2 }  
end  
end

puts A.new.x, B.new.x

Cheers

robert
