# Is there a way to list the classes defined in a module?

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-to-list-the-classes-defined-in-a-module/182274>\
**Category:** Ruby\
**Created:** [January 25, 2010, 9:24pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-list-the-classes-defined-in-a-module/182274 "2010-01-25T21:24:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [January 25, 2010, 9:24pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-list-the-classes-defined-in-a-module/182274/1 "2010-01-25T21:24:47Z")

</div>

Suppose I have a module that’s essentially used as a namespace, like  
this:

module Castle

class King \< Royalty; end

class Queen \< Royalty; end

class Guard \< Commoner; end

class Dragon\_keeper \< Commoner; end

end

Is there some way I could later create a list, say Castle\_dwellers that  
would come out with all the classes defined therein? I would expect an  
array, something like [:King,:Queen,:Guard,:Dragon\_keeper]. Or would I  
have to add them ‘manually’ to the list?

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [January 25, 2010, 9:27pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-list-the-classes-defined-in-a-module/182274/2 "2010-01-25T21:27:11Z")

</div>

Michael W. wrote:

> Suppose I have a module that’s essentially used as a namespace, like  
> this:
> 
> module Castle

Castle.constants

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [January 26, 2010, 5:20am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-list-the-classes-defined-in-a-module/182274/3 "2010-01-26T05:20:49Z")

</div>

> Castle.constants

Pretty much. This will also return any other constants you make in the  
namespace – for example, if you had Castle::Location = “Scotland”,  
you’d also get “Location” back. If that causes you problems, you can  
do:

irb(main):034:0\> Castle.constants.select{|e|Castle.const\_get  
(e).kind\_of? Class}  
=\> [“King”, “Guard”, “Queen”, “Dragon\_keeper”, “Royalty”, “Commoner”]
