Skip to content

Instantly share code, notes, and snippets.

@rwilcox
Created February 12, 2011 17:49
Show Gist options
  • Save rwilcox/823919 to your computer and use it in GitHub Desktop.
Save rwilcox/823919 to your computer and use it in GitHub Desktop.
Returns all classes that have (a class) in their subclass/ancestor chain
class Base; end;
class Parent < Base; end
class Grandchild < Parent; end
def subclasses_of(a_class)
classes = []
ObjectSpace.each_object(Class) do |klass|
decendents = []
current_class = klass
while true do
superc = current_class.superclass
break if superc.nil?
decendents << superc
current_class = superc
end
if decendents.include? a_class
classes << klass
end
end
classes
end
things = subclasses_of(Base) # or umm ActiveRecord::Base ;)
puts things
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment