# About finding subclasses

**URL:** <https://www.ruby-forum.com/t/about-finding-subclasses/92048>\
**Category:** Ruby\
**Created:** [April 15, 2007, 8:56am UTC](https://www.ruby-forum.com/t/about-finding-subclasses/92048 "2007-04-15T08:56:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [April 15, 2007, 8:56am UTC](https://www.ruby-forum.com/t/about-finding-subclasses/92048/1 "2007-04-15T08:56:37Z")

</div>

for a given class, find all subclasses

## i found the solution (by pit capitain) at [http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/e812c7cef446a96](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/e812c7cef446a96) , it works, but i don’t understand it.

require “enumerator”

def get\_subclasses(klass)  
ObjectSpace.enum\_for(:each\_object, class \<\< klass; self;  
end).to\_a  
end

* * *

i don’t understand what “class \<\< klass; self; end” does. can someone  
rewrite and make it longer?

Dorren

---

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [April 15, 2007, 10:03am UTC](https://www.ruby-forum.com/t/about-finding-subclasses/92048/2 "2007-04-15T10:03:43Z")

</div>

On 4/15/07, Dorren [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end).to\_a  
> end
> 
> * * *
> 
> i don’t understand what “class \<\< klass; self; end” does. can someone  
> rewrite and make it longer?

There are two potentially confusing bits about that very elegant piece  
of code.

One is the use of enumerator and enum\_for, which can be pretty easily  
unrolled. However, it may not be clear from that code that the hard  
work is actually being done by ObjectSpace.each\_object which accepts a  
parameter to use as a ‘filter’

The other confusing bit could be the use of the “class \<\< object”  
form, which is described as such (in the pickaxe):

… the class \<\<obj notation, which basically says “build me a new  
class just for object obj.” …

Thats one way of putting it. Another way is presented in this article  
(which is the explanation that makes the most sense to me):  
[http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html](http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html)

Here is a ‘longer’ version of the above like you asked for, though,  
I’m not sure it’s any more clear:  
#################################  
class Object

# The hidden singleton lurks behind everyone

def metaclass  
class \<\< self  
return self  
end  
end  
end

def get\_subclasses(klass)  
the\_subclasses = []

ObjectSpace.each\_object( klass.metaclass ) do |object|  
the\_subclasses.push object  
end

return the\_subclasses  
end

class A  
end

class B \< A  
end

p get\_subclasses( A )  
#################################

Hope that helps,  
-Harold

---

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [April 16, 2007, 12:26am UTC](https://www.ruby-forum.com/t/about-finding-subclasses/92048/3 "2007-04-16T00:26:18Z")

</div>

Metaclass, that’s new to me.I have to re-read the pick-axe book and  
that website, and has a little better understanding now.

Thanks all.

Dorren

---

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [April 16, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/about-finding-subclasses/92048/4 "2007-04-16T20:25:21Z")

</div>

On 4/15/07, eden li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Dang, you beat me to it. I had typed up a similar response. Here’s a  
> minor tweak though.
> 
> On Apr 15, 4:02 pm, “Harold H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > … the class \<\<obj notation, which basically says “build me a new  
> > class just for object obj.” …
> 
> As far as I can tell, all objects (and thus all classes) already have  
> this singleton class. “class \<\< klass; self; end” is just a way to  
> get at it.

Well logically at least. For regular objects a singleton class isn’t  
created until it’s needed.

So:

a = “abc”  
#at this point there is no singleton class for the instance of String  
referenced by a  
def a.foo  
“foo”  
end  
#at this point it does, the singleton class was created to hold the  
method.

Of course you can’t ever see the lack of a singleton class, at least  
from ruby code, in order to do so requires an extension.

Classes get their singleton classes when they are created.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [April 16, 2007, 10:10pm UTC](https://www.ruby-forum.com/t/about-finding-subclasses/92048/5 "2007-04-16T22:10:35Z")

</div>

Harold H. wrote:

> > ```
> > ObjectSpace.enum_for(:each_object, class << klass; self;
> > 
> > ```
> 
> There are two potentially confusing bits about that very elegant piece of code.  
> class just for object obj." …  
> def metaclass  
> the\_subclasses.push object
> 
> p get\_subclasses( A )  
> #################################
> 
> Hope that helps,  
> -Harold

Wow, why’s just twisted my brain… I will have to read his article  
again in a few days, and the maybe again and again until I really may  
get it… but now I just need a drink…

---

<div class="post-metadata">

**Author:** ![Dorren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@Dorren](https://www.ruby-forum.com/u/Dorren)\
**Post date:** [April 15, 2007, 10:40am UTC](https://www.ruby-forum.com/t/about-finding-subclasses/92048/6 "2007-04-15T10:40:45Z")

</div>

Dang, you beat me to it. I had typed up a similar response. Here’s a  
minor tweak though.

On Apr 15, 4:02 pm, “Harold H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> … the class \<\<obj notation, which basically says “build me a new  
> class just for object obj.” …

As far as I can tell, all objects (and thus all classes) already have  
this singleton class. “class \<\< klass; self; end” is just a way to  
get at it. why’s article defines Object#metaclass as an easy way to  
shorthand this long form syntax.

So, it’s pretty clear from the docs that ObjectSpace#each\_object takes  
a module/class as a filter and will return only objects which are  
subclasses of said module/class. If you hand it a metaclass, it does  
simply filters for objects which have metaclasses matching the same  
criteria.

> > class A; end  
> > =\> nil  
> > class B \< A; end  
> > =\> nil  
> > metaclass = class \<\< A; self; end  
> > =\> #Class:A  
> > B.kind\_of?(metaclass)  
> > =\> true  
> > B.new.kind\_of?(metaclass)  
> > =\> false

So ObjectSpace#each\_object will end up returning only classes  
subclassed from the given “klass” since #kind\_of? will hold true for  
only those objects matching that criteria.

I think. 😉
