# How to get the list of all classes?

**URL:** https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850
**Category:** ruby-talk
**Created:** [4 September 2003 19:36 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850 "2003-09-04T19:36:26Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Tobi\_Reif](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Tobi\_Reif](https://rubytalk.org/u/Tobi_Reif)
#### Post date: [4 September 2003 19:36 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/1 "2003-09-04T19:36:26Z")

</div>

Hi

I’m writing a syntax highlighting program [code] for Ruby code listings  
in XML (eg DocBook) documents.

To get the global variables I do  
puts Kernel.global\_variables  
and to get the names of some common methods I do  
puts methods  
and  
puts Kernel.methods  
.

How to get the names of all classes? (all built-in classes or all available)  
I need to get “File”, “Hash”, etc.

Any other wordlist-returning snippets are welcome too.

TIA,  
Tobi

[code]  
XSLT:  
[http://www.pinkjuice.com/howto/vimxml/xslt/tinydbk2xhtml/markup\_ruby.xslt](http://www.pinkjuice.com/howto/vimxml/xslt/tinydbk2xhtml/markup_ruby.xslt)  
sample:  
[http://www.pinkjuice.com/howto/vimxml/setup.xml#jing](http://www.pinkjuice.com/howto/vimxml/setup.xml#jing)  
(scroll down)

> **···**
>
> –  
> [http://www.pinkjuice.com/](http://www.pinkjuice.com/)

---

<div class="post-metadata">

### Author: ![Wesley\_J\_Landaker](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@Wesley\_J\_Landaker](https://rubytalk.org/u/Wesley_J_Landaker)
#### Post date: [4 September 2003 19:41 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/2 "2003-09-04T19:41:13Z")

</div>

Apparently, Tobias Reif recently wrote:

> Hi
> 
> I’m writing a syntax highlighting program [code] for Ruby code listings  
> in XML (eg DocBook) documents.
> 
> To get the global variables I do  
> puts Kernel.global\_variables  
> and to get the names of some common methods I do  
> puts methods  
> and  
> puts Kernel.methods  
> .
> 
> How to get the names of all classes? (all built-in classes or all  
> available)  
> I need to get “File”, “Hash”, etc.

Here is one way:

$ ruby -e ‘x = ; ObjectSpace.each\_object { |o| x \<\< o if o.class ==  
Class }; p x’  
[UnboundMethod, Method, Binding, Proc, SystemStackError, LocalJumpError,  
Struct::Tms, Process::Status, Time, Dir, File::Stat, File, IO, EOFError,  
IOError, Range, MatchData, Regexp, RegexpError, Struct, Hash, Array,  
Bignum, Float, Fixnum, Integer, Numeric, FloatDomainError,  
ZeroDivisionError, ThreadGroup, Continuation, Thread, ThreadError,  
Errno::EDQUOT, Errno::EREMOTEIO, Errno::EISNAM, Errno::ENAVAIL,  
Errno::ENOTNAM, Errno::EUCLEAN, Errno::ESTALE, Errno::EINPROGRESS,  
Errno::EALREADY, Errno::EHOSTUNREACH, Errno::EHOSTDOWN,  
Errno::ECONNREFUSED, Errno::ETIMEDOUT, Errno::ETOOMANYREFS,  
Errno::ESHUTDOWN, Errno::ENOTCONN, Errno::EISCONN, Errno::ENOBUFS,  
Errno::ECONNRESET, Errno::ECONNABORTED, Errno::ENETRESET,  
Errno::ENETUNREACH, Errno::ENETDOWN, Errno::EADDRNOTAVAIL,  
Errno::EADDRINUSE, Errno::EAFNOSUPPORT, Errno::EPFNOSUPPORT,  
Errno::EOPNOTSUPP, Errno::ESOCKTNOSUPPORT, Errno::EPROTONOSUPPORT,  
Errno::ENOPROTOOPT, Errno::EPROTOTYPE, Errno::EMSGSIZE,  
Errno::EDESTADDRREQ, Errno::ENOTSOCK, Errno::EUSERS, Errno::ESTRPIPE,  
Errno::ERESTART, Errno::EILSEQ, Errno::ELIBEXEC, Errno::ELIBMAX,  
Errno::ELIBSCN, Errno::ELIBBAD, Errno::ELIBACC, Errno::EREMCHG,  
Errno::EBADFD, Errno::ENOTUNIQ, Errno::EOVERFLOW, Errno::EBADMSG,  
Errno::EDOTDOT, Errno::EMULTIHOP, Errno::EPROTO, Errno::ECOMM,  
Errno::ESRMNT, Errno::EADV, Errno::ENOLINK, Errno::EREMOTE, Errno::ENOPKG,  
Errno::ENONET, Errno::ENOSR, Errno::ETIME, Errno::ENODATA, Errno::ENOSTR,  
Errno::EBFONT, Errno::EBADSLT, Errno::EBADRQC, Errno::ENOANO,  
Errno::EXFULL, Errno::EBADR, Errno::EBADE, Errno::EL2HLT, Errno::ENOCSI,  
Errno::EUNATCH, Errno::ELNRNG, Errno::EL3RST, Errno::EL3HLT,  
Errno::EL2NSYNC, Errno::ECHRNG, Errno::EIDRM, Errno::ENOMSG, Errno::ELOOP,  
Errno::ENOTEMPTY, Errno::ENOSYS, Errno::ENOLCK, Errno::ENAMETOOLONG,  
Errno::EDEADLK, Errno::ERANGE, Errno::EDOM, Errno::EPIPE, Errno::EMLINK,  
Errno::EROFS, Errno::ESPIPE, Errno::ENOSPC, Errno::EFBIG, Errno::ETXTBSY,  
Errno::ENOTTY, Errno::EMFILE, Errno::ENFILE, Errno::EINVAL, Errno::EISDIR,  
Errno::ENOTDIR, Errno::ENODEV, Errno::EXDEV, Errno::EEXIST, Errno::EBUSY,  
Errno::ENOTBLK, Errno::EFAULT, Errno::EACCES, Errno::ENOMEM,  
Errno::EAGAIN, Errno::ECHILD, Errno::EBADF, Errno::ENOEXEC, Errno::E2BIG,  
Errno::ENXIO, Errno::EIO, Errno::EINTR, Errno::ESRCH, Errno::ENOENT,  
Errno::EPERM, SystemCallError, NoMemoryError, SecurityError, RuntimeError,  
NotImplementedError, LoadError, SyntaxError, ScriptError, NoMethodError,  
NameError, RangeError, IndexError, ArgumentError, TypeError,  
StandardError, Interrupt, SignalException, fatal, SystemExit, Exception,  
String, FalseClass, TrueClass, Data, Symbol, NilClass, Class, Module,  
Object]

---

<div class="post-metadata">

### Author: ![Tobi\_Reif](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Tobi\_Reif](https://rubytalk.org/u/Tobi_Reif)
#### Post date: [5 September 2003 21:39 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/3 "2003-09-05T21:39:11Z")

</div>

How to get the list of names which includes “print”?

TIA,  
Tobi

I wrote:

> I’m writing a syntax highlighting program [code] for Ruby code  
> listings in XML (eg DocBook) documents.  
> […]

> **···**
>
> > Any other wordlist-returning snippets are welcome too.
> 
> –  
> [http://www.pinkjuice.com/](http://www.pinkjuice.com/)

---

<div class="post-metadata">

### Author: ![Ryan\_Pavlik](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@Ryan\_Pavlik](https://rubytalk.org/u/Ryan_Pavlik)
#### Post date: [4 September 2003 20:01 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/4 "2003-09-04T20:01:50Z")

</div>

\> Here is one way: \> \> $ ruby -e 'x = []; ObjectSpace.each\_object { |o| x \< Class }; p x'

This is probably quicker if you do:

```
x = []
ObjectSpace.each_object(Class) { |c| x << c }

```

…so you don’t have to iterate through thousands of non-Class objects.

> **···**
>
> On Fri, 5 Sep 2003 04:41:13 +0900 “Wesley J. Landaker” [wjl@icecavern.net](mailto:wjl@icecavern.net) wrote:
> 
> –  
> Ryan Pavlik [rpav@mephle.com](mailto:rpav@mephle.com)
> 
> “So… I’m not a total bad-ass fueled by relentless evil?” - 8BT

---

<div class="post-metadata">

### Author: ![Tobi\_Reif](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Tobi\_Reif](https://rubytalk.org/u/Tobi_Reif)
#### Post date: [5 September 2003 21:43 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/5 "2003-09-05T21:43:11Z")

</div>

Sorry, please ignore the previous post.  
(forgot to look in Kernel.methods)

Tobi

> How to get the list of names which includes “print”?  
> […]

> **···**
>
> –  
> [http://www.pinkjuice.com/](http://www.pinkjuice.com/)

---

<div class="post-metadata">

### Author: ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)
#### Post date: [4 September 2003 20:46 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/6 "2003-09-04T20:46:51Z")

</div>

It seems odd to me that ObjectSpace doesn’t mixin Enumerable, but  
that’s easily remedied:

```
module ObjectSpace
    class << self
        include Enumerable
	def each(*args, &block) each_object(*args, &block) end
    end
end

```

Then you can just do this:

```
ObjectSpace.find_all { |c| c.class == Class }

```

Although it is still, as noted above, probably less efficient than  
passing Class to each\_object. But it’s an aesthetic improvement,  
I think. I mean, the idea of having to iterate over  
a collection with a block that does nothing but an array append, just  
to get the collection into array form, seems very backwards to me.

-Mark

> **···**
>
> On Fri, Sep 05, 2003 at 05:01:50AM +0900, Ryan Pavlik wrote:
> 
> > > $ ruby -e ‘x = ; ObjectSpace.each\_object { |o| x \<\< o if o.class ==  
> > > Class }; p x’
> > 
> > This is probably quicker if you do:
> > 
> > ```
> > x = []
> > ObjectSpace.each_object(Class) { |c| x << c }
> > 
> > ```
> > 
> > …so you don’t have to iterate through thousands of non-Class objects.

---

<div class="post-metadata">

### Author: ![Wesley\_J\_Landaker](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@Wesley\_J\_Landaker](https://rubytalk.org/u/Wesley_J_Landaker)
#### Post date: [4 September 2003 20:57 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/7 "2003-09-04T20:57:50Z")

</div>

Apparently, Mark J. Reed recently wrote:

> **···**
>
> > On Fri, Sep 05, 2003 at 05:01:50AM +0900, Ryan Pavlik wrote:
> > 
> > > > $ ruby -e ‘x = ; ObjectSpace.each\_object { |o| x \<\< o if o.class ==  
> > > > Class }; p x’
> > > 
> > > This is probably quicker if you do:
> > > 
> > > ```
> > > x = []
> > > ObjectSpace.each_object(Class) { |c| x << c }
> > > 
> > > ```
> > > 
> > > …so you don’t have to iterate through thousands of non-Class objects.
> > 
> > It seems odd to me that ObjectSpace doesn’t mixin Enumerable, but  
> > that’s easily remedied:
> > 
> > module ObjectSpace  
> > class \<\< self  
> > include Enumerable  
> > def each(\*args, &block) each\_object(\*args, &block) end  
> > end  
> > end
> > 
> > Then you can just do this:
> > 
> > ObjectSpace.find\_all { |c| c.class == Class }
> > 
> > Although it is still, as noted above, probably less efficient than  
> > passing Class to each\_object. But it’s an aesthetic improvement,  
> > I think. I mean, the idea of having to iterate over  
> > a collection with a block that does nothing but an array append, just  
> > to get the collection into array form, seems very backwards to me.
> 
> I agree that having ObjectSpace enumerable would make sense. Actually, the  
> first thing I tried when I saw the original message was:
> 
> ObjectSpace.select { |x| x.class == Class }
> 
> … but then I realized that it wasn’t Enumerable.
> 
> Wes
> 
> Wes

---

<div class="post-metadata">

### Author: ![Jason\_Creighton3](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jason\_Creighton3](https://rubytalk.org/u/Jason_Creighton3)
#### Post date: [5 September 2003 04:28 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/8 "2003-09-05T04:28:49Z")

</div>

> > > $ ruby -e ‘x = ; ObjectSpace.each\_object { |o| x \<\< o if o.class ==  
> > > Class }; p x’
> > 
> > This is probably quicker if you do:
> > 
> > ```
> > x = []
> > ObjectSpace.each_object(Class) { |c| x << c }
> > 
> > ```
> > 
> > …so you don’t have to iterate through thousands of non-Class objects.
> 
> It seems odd to me that ObjectSpace doesn’t mixin Enumerable, but  
> that’s easily remedied:

Yes, that’s seemed odd to me at times as well.

> module ObjectSpace  
> class \<\< self  
> include Enumerable  
> def each(\*args, &block) each\_object(\*args, &block) end  
> end  
> end

err…why don’t you just alias each to each\_object?

module ObjectSpace  
class \<\< self  
include Enumerable  
alias :each :each\_object  
end  
end

No mess, no fuss, no greasy aftertaste.

> Then you can just do this:
> 
> ObjectSpace.find\_all { |c| c.class == Class }
> 
> Although it is still, as noted above, probably less efficient than  
> passing Class to each\_object. But it’s an aesthetic improvement,  
> I think. I mean, the idea of having to iterate over  
> a collection with a block that does nothing but an array append, just  
> to get the collection into array form, seems very backwards to me.

It only seems backwards if you’re used to Ruby. 🙂

Jason Creighton

> **···**
>
> On Thu, 04 Sep 2003 20:37:32 GMT “Mark J. Reed” [markjreed@mail.com](mailto:markjreed@mail.com) wrote:
> 
> > On Fri, Sep 05, 2003 at 05:01:50AM +0900, Ryan Pavlik wrote:

---

<div class="post-metadata">

### Author: ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)
#### Post date: [5 September 2003 13:09 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/9 "2003-09-05T13:09:45Z")

</div>

MJR = me  
JC = Jason Creighton

> module ObjectSpace  
> class \<\< self  
> include Enumerable  
> def each(\*args, &block) each\_object(\*args, &block) end  
> end  
> end

> err…why don’t you just alias each to each\_object?

> module ObjectSpace  
> class \<\< self  
> include Enumerable  
> alias :each :each\_object  
> end  
> end

Uhm . . . because it’s too easy?  
I wanted to show off my understanding of passing &blocks around?  
Maybe I’m a firm believer in TMTOWTDI?  
Would you believe . . . I just forgot about alias? 🙂

Thanks.

-Mark

---

<div class="post-metadata">

### Author: ![David\_A\_Black2](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_A\_Black2](https://rubytalk.org/u/David_A_Black2)
#### Post date: [5 September 2003 13:32 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/10 "2003-09-05T13:32:45Z")

</div>

Hi –

> > ```
> > module ObjectSpace
> > class << self
> > include Enumerable
> > def each(*args, &block) each_object(*args, &block) end
> > end
> > end
> > 
> > ```
> 
> err…why don’t you just alias each to each\_object?
> 
> module ObjectSpace  
> class \<\< self  
> include Enumerable  
> alias :each :each\_object  
> end  
> end

Or even:

class \<\< ObjectSpace  
include Enumerable  
alias :each :each\_object  
end

Or, wandering into arguably too-compressed territory:

class \<\< ObjectSpace.extend(Enumerable)  
alias :each :each\_object  
end

David

> **···**
>
> On Fri, 5 Sep 2003, Jason Creighton wrote:
> 
> > On Thu, 04 Sep 2003 20:37:32 GMT \> “Mark J. Reed” [markjreed@mail.com](mailto:markjreed@mail.com) wrote:
> 
> –  
> David Alan Black  
> home: [dblack@superlink.net](mailto:dblack@superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)

---

<div class="post-metadata">

### Author: ![Thomas\_Adam2](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Thomas\_Adam2](https://rubytalk.org/u/Thomas_Adam2)
#### Post date: [5 September 2003 13:19 UTC](https://rubytalk.org/t/how-to-get-the-list-of-all-classes/7850/11 "2003-09-05T13:19:10Z")

</div>

— “Mark J. Reed” [markjreed@mail.com](mailto:markjreed@mail.com) wrote: \> MJR = me

> JC = Jason Creighton
> 
> > ```
> > module ObjectSpace
> > class << self
> > include Enumerable
> > def each(*args, &block) each_object(*args, &block) end
> > end
> > end
> > 
> > ```
> 
> > err…why don’t you just alias each to each\_object?
> 
> > module ObjectSpace  
> > class \<\< self  
> > include Enumerable  
> > alias :each :each\_object  
> > end  
> > end
> 
> Uhm . . . because it’s too easy?

I agree with Jason here, alias is the way to go, it even speeds it up on  
my machine, odd as it seems.

> I wanted to show off my understanding of passing &blocks around?  
> Maybe I’m a firm believer in TMTOWTDI?

TMTOWTDI is a perl idiom 🙂

> Would you believe . . . I just forgot about alias? 🙂

It happens 🙂 I have aliases too: people call me “percy” from time to  
time, depending on who’s asking 🙂

– Thomas Adam

> **···**
>
> =====  
> Thomas Adam
> 
> “The Linux Weekend Mechanic” – [www.linuxgazette.com](http://www.linuxgazette.com)
> 
> * * *
> 
> Want to chat instantly with your online friends? Get the FREE Yahoo!  
> Messenger [http://mail.messenger.yahoo.co.uk](http://mail.messenger.yahoo.co.uk)
