# Module children

**URL:** <https://www.ruby-forum.com/t/module-children/182716>\
**Category:** Ruby\
**Created:** [February 1, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/module-children/182716 "2010-02-01T20:50:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![camilleroux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@camilleroux](https://www.ruby-forum.com/u/camilleroux)\
**Post date:** [February 1, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/module-children/182716/1 "2010-02-01T20:50:31Z")

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

---

<div class="post-metadata">

**Author:** ![camilleroux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@camilleroux](https://www.ruby-forum.com/u/camilleroux)\
**Post date:** [February 1, 2010, 10:05pm UTC](https://www.ruby-forum.com/t/module-children/182716/2 "2010-02-01T22:05:44Z")

</div>

On Feb 1, 8:50 pm, Camille R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/.

Here’s a quick and dirty implementation using ObjectSpace

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

classes = []  
ObjectSpace.each\_object do |obj|  
classes \<\< obj if Module === obj  
end

p classes.select {|c| c.name =~ /^LibraryWrapperThing:😕 }

# =\> [LibraryWrapperThing::Dog, LibraryWrapperThing::Fish, …]

---

<div class="post-metadata">

**Author:** ![camilleroux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@camilleroux](https://www.ruby-forum.com/u/camilleroux)\
**Post date:** [February 1, 2010, 10:07pm UTC](https://www.ruby-forum.com/t/module-children/182716/3 "2010-02-01T22:07:29Z")

</div>

On Tue, Feb 02, 2010 at 04:50:34AM +0900, Camille R. 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?

> > 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:** ![camilleroux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@camilleroux](https://www.ruby-forum.com/u/camilleroux)\
**Post date:** [February 1, 2010, 10:11pm UTC](https://www.ruby-forum.com/t/module-children/182716/4 "2010-02-01T22:11:00Z")

</div>

On Feb 1, 10:06 pm, Aaron P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > module Foo; A = 1; class B; end; module C; end end  
> > > =\> nil  
> > > Foo.constants.find\_all { |k| Class === Foo.const\_get(k) }
> 
> =\> [“B”]

+1 🙂

---

<div class="post-metadata">

**Author:** ![camilleroux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@camilleroux](https://www.ruby-forum.com/u/camilleroux)\
**Post date:** [February 1, 2010, 10:50pm UTC](https://www.ruby-forum.com/t/module-children/182716/5 "2010-02-01T22:50:49Z")

</div>

On Feb 1, 2010, at 13:06 , Aaron P. wrote:

> On Tue, Feb 02, 2010 at 04:50:34AM +0900, Camille R. 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?
> 
> > > 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]

---

<div class="post-metadata">

**Author:** ![camilleroux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@camilleroux](https://www.ruby-forum.com/u/camilleroux)\
**Post date:** [February 1, 2010, 11:34pm UTC](https://www.ruby-forum.com/t/module-children/182716/6 "2010-02-01T23:34:44Z")

</div>

On Feb 1, 2010, at 13:55 , James Edward G. II wrote:

> =\> [Foo::B]  
> Ah! I always forget about that one. Good catch.

---

<div class="post-metadata">

**Author:** ![camilleroux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@camilleroux](https://www.ruby-forum.com/u/camilleroux)\
**Post date:** [February 1, 2010, 10:56pm UTC](https://www.ruby-forum.com/t/module-children/182716/7 "2010-02-01T22:56:06Z")

</div>

On Feb 1, 2010, at 3:38 PM, Ryan D. wrote:

> > =\> 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 G. II
