# Module children

**URL:** https://rubytalk.org/t/module-children/57221
**Category:** ruby-talk
**Created:** [1 February 2010 19:50 UTC](https://rubytalk.org/t/module-children/57221 "2010-02-01T19:50:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Camille\_Cr](https://avatars.discourse-cdn.com/v4/letter/c/eb9ed0/32.png) [@Camille\_Cr](https://rubytalk.org/u/Camille_Cr)
#### Post date: [1 February 2010 19:50 UTC](https://rubytalk.org/t/module-children/57221/1 "2010-02-01T19:50:34Z")

</div>

Hello,

I'd like to get the list of all the classes contained in a module.  
What's the simple way to do that?

Thank you

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

---

<div class="post-metadata">

### Author: ![August\_Lilleaas](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@August\_Lilleaas](https://rubytalk.org/u/August_Lilleaas)
#### Post date: [1 February 2010 21:05 UTC](https://rubytalk.org/t/module-children/57221/2 "2010-02-01T21:05:06Z")

</div>

Here's a quick and dirty implementation using ObjectSpace

module LibraryWrapperThing  
&nbsp;&nbsp;class UtilityClassA; end  
&nbsp;&nbsp;class Fish; end  
&nbsp;&nbsp;class Dog; end  
end  
class NotMe; end

classes =   
ObjectSpace.each\_object do |obj|  
&nbsp;&nbsp;classes \<\< obj if Module === obj  
end

p classes.select {|c| c.name =~ /^LibraryWrapperThing:😕 }  
# =\> [LibraryWrapperThing::Dog, LibraryWrapperThing::Fish, ...]

> **···**
>
> On Feb 1, 8:50 pm, Camille Roux \<roux.cami...@gmail.com\> wrote:
> 
> > Hello,
> > 
> > I'd like to get the list of all the classes contained in a module.  
> > What's the simple way to do that?
> > 
> > Thank you  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

### Author: ![Aaron\_Patterson1](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@Aaron\_Patterson1](https://rubytalk.org/u/Aaron_Patterson1)
#### Post date: [1 February 2010 21:06 UTC](https://rubytalk.org/t/module-children/57221/3 "2010-02-01T21:06:57Z")

</div>

> Hello,
> 
> I'd like to get the list of all the classes contained in a module.  
> What's the simple way to do that?

> > module Foo; A = 1; class B; end; module C; end end

=\> nil

> > Foo.constants.find\_all { |k| Class === Foo.const\_get(k) }

=\> ["B"]

> **···**
>
> On Tue, Feb 02, 2010 at 04:50:34AM +0900, Camille Roux wrote:
> 
> --  
> Aaron Patterson  
> [http://tenderlovemaking.com/](http://tenderlovemaking.com/)

---

<div class="post-metadata">

### Author: ![August\_Lilleaas](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@August\_Lilleaas](https://rubytalk.org/u/August_Lilleaas)
#### Post date: [1 February 2010 21:10 UTC](https://rubytalk.org/t/module-children/57221/4 "2010-02-01T21:10:11Z")

</div>

+1 🙂

> **···**
>
> On Feb 1, 10:06 pm, Aaron Patterson \<aa...@tenderlovemaking.com\> wrote:
> 
> > \>\> module Foo; A = 1; class B; end; module C; end end  
> > =\> nil  
> > \>\> Foo.constants.find\_all { |k| Class === Foo.const\_get(k) }
> > 
> > =\> ["B"]

---

<div class="post-metadata">

### Author: ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)
#### Post date: [1 February 2010 21:38 UTC](https://rubytalk.org/t/module-children/57221/5 "2010-02-01T21:38:56Z")

</div>

> > Hello,
> > 
> > I'd like to get the list of all the classes contained in a module.  
> > What's the simple way to do that?
> 
> > > module Foo; A = 1; class B; end; module C; end end
> 
> =\> nil
> 
> > > Foo.constants.find\_all { |k| Class === Foo.const\_get(k) }
> 
> =\> ["B"]

Personally, I usually map first:

> > Foo.constants.map { |s| Foo.const\_get s }.find\_all { |k| Class === k }

=\> [Foo::B]

> **···**
>
> On Feb 1, 2010, at 13:06 , Aaron Patterson wrote:
> 
> > On Tue, Feb 02, 2010 at 04:50:34AM +0900, Camille Roux wrote:

---

<div class="post-metadata">

### Author: ![JEG2](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@JEG2](https://rubytalk.org/u/JEG2)
#### Post date: [1 February 2010 21:55 UTC](https://rubytalk.org/t/module-children/57221/6 "2010-02-01T21:55:26Z")

</div>

> > > Hello,
> > > 
> > > I'd like to get the list of all the classes contained in a module.  
> > > What's the simple way to do that?
> > 
> > > > module Foo; A = 1; class B; end; module C; end end
> > 
> > =\> nil
> > 
> > > > Foo.constants.find\_all { |k| Class === Foo.const\_get(k) }
> > 
> > =\> ["B"]
> 
> Personally, I usually map first:
> 
> > > Foo.constants.map { |s| Foo.const\_get s }.find\_all { |k| Class === k }
> 
> =\> [Foo::B]

Then you can switch the the under loved grep():

> > Foo.constants.map { |s| Foo.const\_get s }.grep(Class)

=\> [Foo::B]

James Edward Gray II

> **···**
>
> On Feb 1, 2010, at 3:38 PM, Ryan Davis wrote:
> 
> > On Feb 1, 2010, at 13:06 , Aaron Patterson wrote:
> > 
> > > On Tue, Feb 02, 2010 at 04:50:34AM +0900, Camille Roux wrote:

---

<div class="post-metadata">

### Author: ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)
#### Post date: [1 February 2010 22:34 UTC](https://rubytalk.org/t/module-children/57221/7 "2010-02-01T22:34:23Z")

</div>

Ah! I always forget about that one. Good catch.

> **···**
>
> On Feb 1, 2010, at 13:55 , James Edward Gray II wrote:
> 
> > On Feb 1, 2010, at 3:38 PM, Ryan Davis wrote:
> > 
> > > Personally, I usually map first:
> > > 
> > > > > Foo.constants.map { |s| Foo.const\_get s }.find\_all { |k| Class === k }
> > > 
> > > =\> [Foo::B]
> > 
> > Then you can switch the the under loved grep():
> > 
> > > > Foo.constants.map { |s| Foo.const\_get s }.grep(Class)
> > 
> > =\> [Foo::B]
