# How can I prevent include one module multi-times?

**URL:** <https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821>\
**Category:** ruby-talk\
**Created:** [3 September 2008 02:59 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821 "2008-09-03T02:59:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhao\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@Zhao\_Yi](https://rubytalk.org/u/Zhao_Yi)\
**Post date:** [3 September 2008 02:59 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821/1 "2008-09-03T02:59:06Z")

</div>

I am looking for a method to check whether this module has been  
included. Does Ruby have this feature?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [3 September 2008 04:18 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821/2 "2008-09-03T04:18:48Z")

</div>

# I am looking for a method to check whether this module has been  
# included. Does Ruby have this feature?

you can query the class/module thru #included\_modules

eg,

> class C  
> end

=\> nil

> C.included\_modules

=\> [Kernel]

> module M  
> end

=\> nil

> class C  
> include M  
> end

=\> C

> C.included\_modules

=\> [M, Kernel]

or if you want to query the whole program space, search the ObjectSpace

eg,

> ObjectSpace.each\_object(Module).select{|m| m.class.name == "Module"}

=\> [IRB, Exception2MessageMapper, Marshal, ObjectSpace, GC, Math, Process::Sys, Process::GID, Process::UID, Process, Signal, File::Constants, FileTest, Errno, Precision, Enumerable, Comparable, Kernel, Readline, RubyToken, IRB::Notifier, M, IRB::MethodExtender, IRB::ContextExtender, IRB::ExtendCommandBundle]

kind regards -botp

> **···**
>
> From: Zhao Yi [mailto:youhaodeyi@gmail.com]

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [3 September 2008 05:33 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821/3 "2008-09-03T05:33:35Z")

</div>

Zhao Yi wrote:

> I am looking for a method to check whether this module has been  
> included. Does Ruby have this feature?

Might try parsing Object.constants for the module name.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![TPReal](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@TPReal](https://rubytalk.org/u/TPReal)\
**Post date:** [3 September 2008 06:59 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821/4 "2008-09-03T06:59:44Z")

</div>

Zhao Yi wrote:

> I am looking for a method to check whether this module has been  
> included. Does Ruby have this feature?

What's your question? In the subject you ask 'How can I prevent include  
one module multi-times?' and this is checked automatically without any  
special handling.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Coglan2](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@James\_Coglan2](https://rubytalk.org/u/James_Coglan2)\
**Post date:** [3 September 2008 07:14 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821/5 "2008-09-03T07:14:16Z")

</div>

That's partially correct, but you'll find that the module's included() hook  
gets called repeatedly:

module M  
&nbsp;&nbsp;def self.included(base)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "included M"  
&nbsp;&nbsp;end  
end

class C  
&nbsp;&nbsp;# prints "included M" 3 times  
&nbsp;&nbsp;include M  
&nbsp;&nbsp;include M  
&nbsp;&nbsp;include M  
end

To check whether M is already mixed into C, the expression 'M \> C' returns  
true if C includes M. You could put this check inside M.included and throw  
an exception if it's true.

> **···**
>
> 2008/9/3 Thomas B. \<tpreal@gmail.com\>
> 
> > Zhao Yi wrote:  
> > \> I am looking for a method to check whether this module has been  
> > \> included. Does Ruby have this feature?
> > 
> > What's your question? In the subject you ask 'How can I prevent include  
> > one module multi-times?' and this is checked automatically without any  
> > special handling.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [3 September 2008 08:28 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821/6 "2008-09-03T08:28:36Z")

</div>

Roger Pack wrote:

> Zhao Yi wrote:  
> \> I am looking for a method to check whether this module has been  
> \> included. Does Ruby have this feature?
> 
> Might try parsing Object.constants for the module name.

Why? That only tells you whether that module exists (and it only tells you  
that when the module is in the top-level namespace). It doesn't tell you  
whether that module has been included into anything.

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![James\_Coglan2](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@James\_Coglan2](https://rubytalk.org/u/James_Coglan2)\
**Post date:** [3 September 2008 07:15 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821/7 "2008-09-03T07:15:27Z")

</div>

I should have mentioned that including a module multiple times will not have  
any nasty side effects on the inheritance tree of the including class --  
this is checked for you.

> **···**
>
> 2008/9/3 James Coglan \<jcoglan@googlemail.com\>
> 
> > 2008/9/3 Thomas B. \<tpreal@gmail.com\>
> > 
> > > Zhao Yi wrote:  
> > > \> I am looking for a method to check whether this module has been  
> > > \> included. Does Ruby have this feature?
> > > 
> > > What's your question? In the subject you ask 'How can I prevent include  
> > > one module multi-times?' and this is checked automatically without any  
> > > special handling.
> > 
> > That's partially correct, but you'll find that the module's included() hook  
> > gets called repeatedly:
> > 
> > module M  
> > &nbsp;&nbsp;def self.included(base)  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "included M"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class C  
> > &nbsp;&nbsp;# prints "included M" 3 times  
> > &nbsp;&nbsp;include M  
> > &nbsp;&nbsp;include M  
> > &nbsp;&nbsp;include M  
> > end
> > 
> > To check whether M is already mixed into C, the expression 'M \> C' returns  
> > true if C includes M. You could put this check inside M.included and throw  
> > an exception if it's true.

---

<div class="post-metadata">

**Author:** ![James\_Coglan2](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@James\_Coglan2](https://rubytalk.org/u/James_Coglan2)\
**Post date:** [3 September 2008 08:35 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821/8 "2008-09-03T08:35:23Z")

</div>

Ah, I think I misunderstood the question. I wrote this a while ago to find  
all the descendants of a module:

class Module  
&nbsp;&nbsp;def descendants  
&nbsp;&nbsp;&nbsp;&nbsp;classes =   
&nbsp;&nbsp;&nbsp;&nbsp;ObjectSpace.each\_object do |klass|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next unless Module === klass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;classes \<\< klass if self \> klass  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;classes  
&nbsp;&nbsp;end  
end

So, a module has been included if

mod.descendants.size.nonzero?

> **···**
>
> 2008/9/3 Sebastian Hungerecker \<sepp2k@googlemail.com\>
> 
> > Roger Pack wrote:  
> > \> Zhao Yi wrote:  
> > \> \> I am looking for a method to check whether this module has been  
> > \> \> included. Does Ruby have this feature?  
> > \>  
> > \> Might try parsing Object.constants for the module name.
> > 
> > Why? That only tells you whether that module exists (and it only tells you  
> > that when the module is in the top-level namespace). It doesn't tell you  
> > whether that module has been included into anything.

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [4 September 2008 18:04 UTC](https://rubytalk.org/t/how-can-i-prevent-include-one-module-multi-times/48821/9 "2008-09-04T18:04:04Z")

</div>

> Ah, I think I misunderstood the question. I wrote this a while ago to  
> find  
> all the descendants of a module:

ah--if the problem is determining if a descendant exists, you could  
write an included(instance) method within the included Module to track.  
Cheers.  
-=R

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
