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
Written by Melanie Archer
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#