Created
February 12, 2011 17:49
-
-
Save rwilcox/823919 to your computer and use it in GitHub Desktop.
Returns all classes that have (a class) in their subclass/ancestor chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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