# Get modules that are in a class?

**URL:** <https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852>\
**Category:** Ruby\
**Created:** [July 8, 2007, 4:00am UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852 "2007-07-08T04:00:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 8, 2007, 4:00am UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852/1 "2007-07-08T04:00:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 8, 2007, 4:47am UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852/2 "2007-07-08T04:47:13Z")

</div>

Wayne E. Seguin wrote:

> On Jul 07, 2007, at 22:00 , Aaron S. 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  
def say\_something  
puts “SOMETHING”  
end  
end

class Test  
include TestModule  
end

t = Test.new  
puts t.included\_modules

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 8, 2007, 5:07am UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852/3 "2007-07-08T05:07:55Z")

</div>

On Jul 07, 2007, at 22:47 , Aaron S. wrote:

> end
> 
> t = Test.new  
> puts t.included\_modules

Aaron,

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

module TestModule  
def say\_something  
puts “SOMETHING”  
end  
end

class Test  
include TestModule  
def modules  
self.class.included\_modules  
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  
def modules  
self.class.included\_modules  
end  
end

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

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 8, 2007, 5:10am UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852/4 "2007-07-08T05:10:56Z")

</div>

Aaron S. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 8, 2007, 5:15am UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852/5 "2007-07-08T05:15:49Z")

</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.

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 8, 2007, 5:18am UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852/6 "2007-07-08T05:18:56Z")

</div>

On Jul 07, 2007, at 23:06 , Wayne E. Seguin wrote:

> class Test  
> If you need this exposed for all classes you can expose for the  
> difficult to come up with an optimal solution 🙂  
> Aaron,

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

module TestModule  
def say\_something  
puts “SOMETHING”  
end  
end

class Test  
include TestModule  
end

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

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 8, 2007, 4:16am UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852/7 "2007-07-08T04:16:53Z")

</div>

On Jul 07, 2007, at 22:00 , Aaron S. 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”

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

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 8, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852/8 "2007-07-08T13:33:23Z")

</div>

Hi –

On Sun, 8 Jul 2007, Travis D Warlick Jr wrote:

> > What about within a class?
> > 
> > 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:

“”.class.included\_modules

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

David

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 11, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/get-modules-that-are-in-a-class/108852/9 "2007-07-11T15:20:45Z")

</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.
