From: "Jesús Gabriel y Galán" Date: 2008-11-19T19:12:03+09:00 Subject: Re: Is there a way to know what are the subclasses of a given class ? > On 2008.11.19., at 10:10, Sandro Paganotti wrote: > >> [SOLVED] >> >> I just find out that you can call: >> >> A.constants --> ["B","C"] >> >> Thank you >> >> Sandro >> >> On Wed, Nov 19, 2008 at 10:10 AM, Sandro Paganotti < >> sandro.paganotti@gmail.com> wrote: >> >>> Hello >>> >>> Do you know if there's a trick to retrieve a list of all the subclasses >>> of >>> a given class ? >>> >>> class A >>> class B >>> end >>> class C >>> end >>> end >>> >>> like A.subclasses --> [B,C] ? As has been mentioned, in your example B and C are not subclasses of A. If you want to list all subclasses of a class you can do this: irb(main):001:0> class A irb(main):002:1> class << self irb(main):003:2> attr_accessor :subclasses irb(main):004:2> end irb(main):005:1> def self.inherited child irb(main):006:2> (A.subclasses ||= []) << child irb(main):007:2> end irb(main):008:1> end => nil irb(main):009:0> A.subclasses => nil irb(main):010:0> class B < A; end; irb(main):011:0* A.subclasses => [B] irb(main):012:0> class C < B; end; irb(main):013:0* A.subclasses => [B, C] irb(main):014:0> class D < A; end; irb(main):015:0* A.subclasses => [B, C, D] Hope this helps, Jesus.