# Module include matters?

**URL:** <https://www.ruby-forum.com/t/module-include-matters/63866>\
**Category:** Ruby\
**Created:** [June 20, 2006, 12:32am UTC](https://www.ruby-forum.com/t/module-include-matters/63866 "2006-06-20T00:32:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenzei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7bcc69/32.png) [@zenzei](https://www.ruby-forum.com/u/zenzei)\
**Post date:** [June 20, 2006, 12:32am UTC](https://www.ruby-forum.com/t/module-include-matters/63866/1 "2006-06-20T00:32:45Z")

</div>

Hi,

I was playing with various modules and found something I can’t  
explain. It seems like module inclusion’s order matters ? Here is a  
simple test case :

module B; end  
module C; include B; end

module A; def somemethod; end; end  
#reopening  
module B; include A; end

p B.instance\_methods #=\> [“somemethod”]  
p C.instance\_methods #=\> [] ???

I don’t understand why C doesn’t inherit A’s instance\_methods. Does  
somebody know something about that ?

–  
Cheers,  
zimbatm

> **[oree.ch](http://zimbatm.oree.ch)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![zenzei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7bcc69/32.png) [@zenzei](https://www.ruby-forum.com/u/zenzei)\
**Post date:** [June 20, 2006, 6:32am UTC](https://www.ruby-forum.com/t/module-include-matters/63866/2 "2006-06-20T06:32:17Z")

</div>

Hello,

On Tue, 20 Jun 2006 07:32:22 +0900, Jonas P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I was playing with various modules and found something I can’t  
> explain. It seems like module inclusion’s order matters ? Here is a  
> simple test case :

[Snip example]

> I don’t understand why C doesn’t inherit A’s instance\_methods. Does  
> somebody know something about that ?

Yes, I asked a similar version of this question in [ruby-talk:121118]

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/121118](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/121118)

Pit C. provided a good summary and link to further details in  
[ruby-talk:121191]

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/121191](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/121191)

I hope this helps,  
Zev

---

<div class="post-metadata">

**Author:** ![zenzei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7bcc69/32.png) [@zenzei](https://www.ruby-forum.com/u/zenzei)\
**Post date:** [June 20, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/module-include-matters/63866/3 "2006-06-20T12:20:42Z")

</div>

This is called the “Dyanmic Module Inclusion Problem”. And Pit is  
right, it is an implmentation issue. I think Matz would love to have a  
fix for it, but appearently it is difficult, if not impossible, to do  
so efficiently,

T.

---

<div class="post-metadata">

**Author:** ![zenzei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7bcc69/32.png) [@zenzei](https://www.ruby-forum.com/u/zenzei)\
**Post date:** [June 20, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/module-include-matters/63866/4 "2006-06-20T16:06:31Z")

</div>

Thanks both of you for your insight. I’ve created a small hack to fix  
that issue

class Module

# Weird hack to ensure deep inclusion of modules

def include\_deep(mod)  
include mod  
ancestors[1…-1].select do |a|  
a != mod and not a.ancestors.include?(mod)  
end.each { |a| a.include\_deep(mod) }  
end  
end

–  
Cheers,  
zimbatm

> **[oree.ch](http://zimbatm.oree.ch)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![zenzei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7bcc69/32.png) [@zenzei](https://www.ruby-forum.com/u/zenzei)\
**Post date:** [June 20, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/module-include-matters/63866/5 "2006-06-20T18:56:16Z")

</div>

On 20/06/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hmm… Doesn’t seem to do anything. Notice even without the above one  
> can instantiate a class and the mothod will “magically” showup. From  
> your example:

Hmm right, let me correct that

---

<div class="post-metadata">

**Author:** ![zenzei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7bcc69/32.png) [@zenzei](https://www.ruby-forum.com/u/zenzei)\
**Post date:** [June 20, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/module-include-matters/63866/6 "2006-06-20T19:26:26Z")

</div>

On 20/06/06, Jonas P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 20/06/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hmm… Doesn’t seem to do anything. Notice even without the above one  
> > can instantiate a class and the mothod will “magically” showup. From  
> > your example:
> 
> Hmm right, let me correct that

doh’ I would need to find the children and not the ancestors [C] of  
the module (B) in which in include the other (A)… I guess it’s  
doable by browsing the ObjectSpace but it’s quite expensive. If only  
modules and classes would also list their childrens. Which is also  
doable by overriding Module#included I guess but it’s not really  
reliable isn’t it ?

–  
Cheers,  
zimbatm

> **[oree.ch](http://zimbatm.oree.ch)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![zenzei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7bcc69/32.png) [@zenzei](https://www.ruby-forum.com/u/zenzei)\
**Post date:** [June 20, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/module-include-matters/63866/7 "2006-06-20T18:05:11Z")

</div>

Jonas P. wrote:

> end  
> Hmm… Doesn’t seem to do anything. Notice even without the above one  
> can instantiate a class and the mothod will “magically” showup. From  
> your example:

class X  
include B  
end

X.new.somemethod

T.
