Here's an inefficient way:
See all descendants of a class in Ruby
An efficient approach would be to use hook inherited
:
class Foo def self.descendants @descendants ||= [] end def self.inherited(descendant) descendants << descendant end end class Bar < Foo; end class Zip < Foo; end Foo.descendants
If you need to know about descendants of descendants, you can return them:
class Foo def self.all_descendants descendants.inject([]) do |all, descendant| (all << descendant) + descendant.all_descendants end end end class Blah < Bar; end Foo.descendants
source share