Ruby: Finding subclasses in your world
Posted Tue, 29 Sep 2009
Use the ObjectSpace class to find all ancestors of a given class.
class Foo; end class Bar < Foo; end class Baz < Foo; end subclasses = ObjectSpace.each_object(Class).select do |klass| klass.ancestors.include?(Foo) and klass != Foo end # prints "[Baz, Bar]" puts subclassesOf course, you could always override Class#inherited instead, but if you don't want to override methods, the above is a reasonable choice.