HOWTO - Get a list of a class's subclasses

  • Ruby

I recently came across a situation where I had an AbstractClass, an I wanted to know all of the classes that had inherited from it. There were lots of implementations on the web, but that weren't exactly what I wanted, or they used ObjectSpace to get ALL the classes, and see if the interesting one was in its ancestors.

I only needed it one-level deep, but it would be fairly easy to extend it for more.

class ParentClass
  def self.subclasses
    @subclasses ||=

  def self.inherited(subclass)
    subclasses << subclass

class ChildA < ParentClass; end
class ChildB < ParentClass; end

# => #<Set: {ChildA, ChildB}>