From: Bruce Williams Date: 2002-12-06T18:34:16+09:00 Subject: Re: Printing subclasses On Friday 06 December 2002 01:37 am, ted wrote: | Can Ruby print the subclasses of a given class? Like the reverse of the | example of Class#superclass in "Reflection, ObjectSace and Distributed | Ruby" in pickaxe. Ted, If you want a list of all subclasses of a certain class, you could implement something similar to the following: class SuperClass SUBS = [] def Superclass.inherited subclass Superclass::SUBS << subclass end end class SubClass1 < SuperClass; end class SubClass2 < SuperClass; end Due to the fact SuperClass.inherited is called for each definition of a subclass (with the subclass given as the argument), the contents of SuperClass::SUBS would then be: [SubClass1, SubClass2] I've seen this used for Plugin classes. Note this approach does not require that the subclasses be instantiated; IIRC using ObjectSpace does. Plus I think this is a little less-heavy handed. Hope that helps, ~ Bruce -- Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com 'It does not require a majority to prevail, but rather an irate, tireless minority keen to set brush fires in people's minds.' -- Samuel Adams