# Is there a way to get or list all available classes?

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-to-get-or-list-all-available-classes/51357>\
**Category:** Ruby\
**Created:** [November 30, 2005, 11:56pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-or-list-all-available-classes/51357 "2005-11-30T23:56:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [November 30, 2005, 11:56pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-or-list-all-available-classes/51357/1 "2005-11-30T23:56:23Z")

</div>

Hello,

topic says it all  
for example irb knows many classes like Dir, File, Process, Thread …  
can I somehow list them, maybe the are kept in some array …  
if it’s possible, can additional classes provided through  
require “foo”  
be listet too?

I hope my question is clear  
thanks in advance

Regards, Daniel

---

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [December 1, 2005, 12:04am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-or-list-all-available-classes/51357/2 "2005-12-01T00:04:27Z")

</div>

Hi Daniel,

Try this:

C:\>irb  
irb(main):001:0\> ObjectSpace.each\_object(Class) {|c| p c}  
Gem::SourceIndex  
OpenSSL::Digest::SHA1  
OpenSSL::Digest::SHA  
Gem::ConsoleUI  
OpenSSL::Digest::RIPEMD160  
Gem::StreamUI  
…etc…etc…etc

Wayne V.  
No Bugs Software  
Ruby and C++ Agile Contract Programming in Silicon Valley

---

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [December 1, 2005, 11:11am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-or-list-all-available-classes/51357/3 "2005-12-01T11:11:44Z")

</div>

Wayne V. wrote:

> OpenSSL::Digest::RIPEMD160  
> Gem::StreamUI  
> …etc…etc…etc

Note, that all approaches using ObjectSpace list only classes known to  
the  
interpreter at this moment. There might be more classes whose files  
haven’t been required / loaded yet. You won’t see them. Also,  
additional  
classes may be generated dynamically.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [December 3, 2005, 10:47am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-or-list-all-available-classes/51357/4 "2005-12-03T10:47:22Z")

</div>

A slightly more hackish solution I think that might also work with  
autoloads (way too early in the morning to go and code it), is to use  
#constants, eval those, select the ones that are classes, recurse,  
rinse, repeat. Mind you, this is potentially a much worse resource hog  
than the ObjectSpace approach.

David V.

---

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [December 1, 2005, 12:52am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-or-list-all-available-classes/51357/5 "2005-12-01T00:52:55Z")

</div>

Daniel Schüle wrote:

> thanks in advance
> 
> Regards, Daniel

classes = []  
ObjectSpace.each\_object {|obj| classes \<\< obj if obj.class == Class}  
classes = classes.sort\_by {|cls| cls.inspect}  
puts classes

---

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [December 5, 2005, 12:08am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-or-list-all-available-classes/51357/6 "2005-12-05T00:08:53Z")

</div>

Hi,

Not sure exactly which list of “classes available” you want, but if  
you wanted to know all the classes available to the interpreter to  
load, then you could conceivably iterate the directories in $:, the  
load path, and look at all .rb files in them and their subdirectories  
and parse them for the classes that they define.

Bob

On Nov 30, 2005, at 2:52 PM, Daniel Schüle wrote:

---

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [December 5, 2005, 1:48am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-or-list-all-available-classes/51357/7 "2005-12-05T01:48:53Z")

</div>

Robert K. wrote:

> > Gem::ConsoleUI
> 
> ```
> robert
> 
> ```

right, and immediate values are kind of important:Fixnum, Symbol,  
TrueClass, FalseClass, NilClass ;-p}
