Last Updated: February 25, 2016
·
362
· mejarc

Discovering the child objects in Ruby

With Rails 3 and up, it's as easy as

ParentClass.subclasses # for the first level down
ParentClass.descendants # for the whole family tree

Without Rails, it takes a little scripting:

class Class
  def descendants
    ObjectSpace.each_object(::Class).select { |klass| klass < self }
  end
end