From: "ara.t.howard" Date: 2008-08-21T23:50:10+09:00 Subject: Re: Find all subclasses of a class/module On Aug 21, 2008, at 7:55 AM, James Coglan wrote: > Hi list, > > I'm trying to find all the classes/modules that descend from a > particular > class/module. Please tell me there's a better way than this: > > http://gist.github.com/6560 > > -- > James Coglan just let ruby track it for you: cfp:~ > cat a.rb class A end class B < A end class C < A end class D < B end p D => D.subclasses p C => C.subclasses p B => B.subclasses p A => A.subclasses p Object => Object.subclasses BEGIN { class Class def inherited other super if defined? super ensure ( @subclasses ||= [] ).push(other).uniq! end def subclasses @subclasses ||= [] @subclasses.inject( [] ) do |list, subclass| list.push(subclass, *subclass.subclasses) end end end } cfp:~ > ruby a.rb {D=>[]} {C=>[]} {B=>[D]} {A=>[B, D, C]} {Object=>[A, B, D, C]} a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama