# Get modules that are in a class?

**URL:** <https://rubytalk.org/t/get-modules-that-are-in-a-class/38923>\
**Category:** ruby-talk\
**Created:** [8 July 2007 02:00 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923 "2007-07-08T02:00:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Aaron\_Smith](https://rubytalk.org/u/Aaron_Smith)\
**Post date:** [8 July 2007 02:00 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923/1 "2007-07-08T02:00:55Z")

</div>

is it possible to find out what modules have been included inside of a  
class?

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

---

<div class="post-metadata">

**Author:** ![Wayne\_E\_Seguin](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Wayne\_E\_Seguin](https://rubytalk.org/u/Wayne_E_Seguin)\
**Post date:** [8 July 2007 02:16 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923/2 "2007-07-08T02:16:23Z")

</div>

Yes Aaron it is, via the method "included\_modules"

[http://www.ruby-doc.org/core/classes/Module.html#M001697](http://www.ruby-doc.org/core/classes/Module.html#M001697)

> **···**
>
> On Jul 07, 2007, at 22:00 , Aaron Smith wrote:
> 
> > is it possible to find out what modules have been included inside of a  
> > class?
> 
> --  
> Wayne E. Seguin  
> Sr. Systems Architect & Systems Admin  
> wayneseguin@gmail.com

---

<div class="post-metadata">

**Author:** ![Aaron\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Aaron\_Smith](https://rubytalk.org/u/Aaron_Smith)\
**Post date:** [8 July 2007 02:47 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923/3 "2007-07-08T02:47:23Z")

</div>

Wayne E. Seguin wrote:

> > is it possible to find out what modules have been included inside of a  
> > class?
> 
> Yes Aaron it is, via the method "included\_modules"
> 
> [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001697)

What about within a class?

module TestModule  
&nbsp;&nbsp;def say\_something  
&nbsp;&nbsp;&nbsp;&nbsp;puts "SOMETHING"  
&nbsp;&nbsp;end  
end

class Test  
&nbsp;&nbsp;include TestModule  
end

t = Test.new  
puts t.included\_modules

> **···**
>
> > On Jul 07, 2007, at 22:00 , Aaron Smith wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Wayne\_E\_Seguin](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Wayne\_E\_Seguin](https://rubytalk.org/u/Wayne_E_Seguin)\
**Post date:** [8 July 2007 03:06 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923/4 "2007-07-08T03:06:51Z")

</div>

Aaron,

you can define a method within your class to expose this like so:

module TestModule  
&nbsp;&nbsp;&nbsp;def say\_something  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "SOMETHING"  
&nbsp;&nbsp;&nbsp;end  
end

class Test  
&nbsp;&nbsp;&nbsp;include TestModule  
&nbsp;&nbsp;&nbsp;def modules  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.included\_modules  
&nbsp;&nbsp;&nbsp;end  
end

t = Test.new  
puts t.modules

If you need this exposed for all classes you can expose for the Object class:

class Object  
&nbsp;&nbsp;&nbsp;def modules  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.included\_modules  
&nbsp;&nbsp;&nbsp;end  
end

Does this help?  
Without knowing what you're trying to accomplish further it's difficult to come up with an optimal solution 🙂

> **···**
>
> On Jul 07, 2007, at 22:47 , Aaron Smith wrote:
> 
> > What about within a class?
> > 
> > module TestModule  
> > &nbsp;&nbsp;def say\_something  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "SOMETHING"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Test  
> > &nbsp;&nbsp;include TestModule  
> > end
> > 
> > t = Test.new  
> > puts t.included\_modules
> 
> --  
> Wayne E. Seguin  
> Sr. Systems Architect & Systems Admin  
> wayneseguin@gmail.com

---

<div class="post-metadata">

**Author:** ![Travis\_D\_Warlick\_Jr](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@Travis\_D\_Warlick\_Jr](https://rubytalk.org/u/Travis_D_Warlick_Jr)\
**Post date:** [8 July 2007 03:10 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923/5 "2007-07-08T03:10:00Z")

</div>

Aaron Smith wrote:

> Wayne E. Seguin wrote:
> 
> > > is it possible to find out what modules have been included inside of a  
> > > class?
> > 
> > Yes Aaron it is, via the method "included\_modules"
> > 
> > [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001697)
> 
> What about within a class?
> 
> module TestModule  
> &nbsp;&nbsp;def say\_something  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "SOMETHING"  
> &nbsp;&nbsp;end  
> end
> 
> class Test  
> &nbsp;&nbsp;include TestModule  
> end
> 
> t = Test.new  
> puts t.included\_modules

Remember that when you include the module, you're including \_all\_ the  
module's methods (including Module#included\_modules)

So, use self.class to get the Class object of the current instance (this  
will work from the included modules also), so you should be able to do:

self.class.included\_modules

And a debugging efficiency tip: use the Array#sort method with the  
Module#included\_modules to sort the list of included modules for easier  
viewing. I use this all the time in IRB. (This also works with  
Class#methods and all the like)

irb(main):001:0\> YourClass.methods.sort

> **···**
>
> > > On Jul 07, 2007, at 22:00 , Aaron Smith wrote:
> 
> --  
> &nbsp;&nbsp;Travis Warlick  
> &nbsp;&nbsp;Operis Systems, LLC  
> &nbsp;&nbsp;Lead Developer

---

<div class="post-metadata">

**Author:** ![Aaron\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Aaron\_Smith](https://rubytalk.org/u/Aaron_Smith)\
**Post date:** [8 July 2007 03:15 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923/6 "2007-07-08T03:15:56Z")

</div>

> And a debugging efficiency tip: use the Array#sort method with the  
> Module#included\_modules to sort the list of included modules for easier  
> viewing. I use this all the time in IRB. (This also works with  
> Class#methods and all the like)
> 
> irb(main):001:0\> YourClass.methods.sort

Thanks. That's perfect.

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

---

<div class="post-metadata">

**Author:** ![Wayne\_E\_Seguin](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@Wayne\_E\_Seguin](https://rubytalk.org/u/Wayne_E_Seguin)\
**Post date:** [8 July 2007 03:18 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923/7 "2007-07-08T03:18:00Z")

</div>

Aaron,

Scratch that last, it's far easier than that:

module TestModule  
&nbsp;&nbsp;&nbsp;def say\_something  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "SOMETHING"  
&nbsp;&nbsp;&nbsp;end  
end

class Test  
&nbsp;&nbsp;&nbsp;include TestModule  
end

t = Test.new  
puts t.class.included\_modules

> **···**
>
> On Jul 07, 2007, at 23:06 , Wayne E. Seguin wrote:
> 
> > Aaron,
> > 
> > you can define a method within your class to expose this like so:
> > 
> > module TestModule  
> > &nbsp;&nbsp;def say\_something  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "SOMETHING"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Test  
> > &nbsp;&nbsp;include TestModule  
> > &nbsp;&nbsp;def modules  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.class.included\_modules  
> > &nbsp;&nbsp;end  
> > end
> > 
> > t = Test.new  
> > puts t.modules
> > 
> > If you need this exposed for all classes you can expose for the Object class:
> > 
> > class Object  
> > &nbsp;&nbsp;def modules  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.class.included\_modules  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Does this help?  
> > Without knowing what you're trying to accomplish further it's difficult to come up with an optimal solution 🙂
> 
> --  
> Wayne E. Seguin  
> Sr. Systems Architect & Systems Admin  
> wayneseguin@gmail.com

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [8 July 2007 11:32 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923/8 "2007-07-08T11:32:49Z")

</div>

Hi --

> Aaron Smith wrote:
> 
> > Wayne E. Seguin wrote:
> > 
> > > > is it possible to find out what modules have been included inside of a  
> > > > class?
> > > 
> > > Yes Aaron it is, via the method "included\_modules"
> > > 
> > > [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001697)
> > 
> > What about within a class?
> > 
> > module TestModule  
> > &nbsp;&nbsp;def say\_something  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "SOMETHING"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Test  
> > &nbsp;&nbsp;include TestModule  
> > end
> > 
> > t = Test.new  
> > puts t.included\_modules
> 
> Remember that when you include the module, you're including \_all\_ the  
> module's methods (including Module#included\_modules)

It's not exactly an inclusion thing. Class objects already respond to  
#included\_modules, because Class inherits from Module.

> So, use self.class to get the Class object of the current instance (this  
> will work from the included modules also), so you should be able to do:
> 
> self.class.included\_modules

That will work with any object:

&nbsp;&nbsp;&nbsp;"".class.included\_modules

etc. It's not dependent on your having included a module.

David

> **···**
>
> On Sun, 8 Jul 2007, Travis D Warlick Jr wrote:
> 
> > > > On Jul 07, 2007, at 22:00 , Aaron Smith wrote:
> 
> --  
> \* Books:  
> &nbsp;&nbsp;&nbsp;RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242\](http://www.awprofessional.com/title/0321509242%5C))  
> &nbsp;&nbsp;&nbsp;RUBY FOR RAILS ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> \* Ruby/Rails training  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![vasudevram](https://avatars.discourse-cdn.com/v4/letter/v/bbce88/32.png) [@vasudevram](https://rubytalk.org/u/vasudevram)\
**Post date:** [11 July 2007 13:17 UTC](https://rubytalk.org/t/get-modules-that-are-in-a-class/38923/9 "2007-07-11T13:17:36Z")

</div>

> YourClass.methods.sort

Yes, that's a useful trick. I use it all the time.  
We can also make up many more such, with a bit of thought.

Another one I use a lot, when I think that some class is likely to  
have a method with some substring in its name, is:

YourClass.methods.grep /substring/

e.g. : String.methods.grep /case/ # to find out what the String method  
name to uppercase (or lowercase) a string, is called.  
or  
"".methods.grep /case/

Vasudev Ram

> **[dancingbison.com](http://ww38.dancingbison.com/)**
>
> Learn Python Programming

> **[jugad's Journal](https://jugad.livejournal.com/)**
>
> Vasudev Ram's blog on software innovation

> **[Conversion of other file formats to PDF](https://sourceforge.net/projects/xtopdf)**
>
> Download Conversion of other file formats to PDF for free. xtopdf: Tools to convert other formats (x) to PDF; x as in math.
