Skip to content

Instantly share code, notes, and snippets.

@tjstankus
Created June 10, 2009 18:11
Show Gist options
  • Save tjstankus/127393 to your computer and use it in GitHub Desktop.
Save tjstankus/127393 to your computer and use it in GitHub Desktop.
module Subclasses
# return a list of the subclasses of a class
def subclasses(direct = false)
classes = []
if direct
ObjectSpace.each_object(Class) do |c|
next unless c.superclass == self
classes << c
end
else
ObjectSpace.each_object(Class) do |c|
next unless c.ancestors.include?(self) and (c != self)
classes << c
end
end
classes
end
end
class Parent
def self.runnable?
false
end
end
class Child < Parent
def self.runnable?
true
end
end
class Grandchild < Child
def self.runnable?
false
end
end
Object.send(:include, Subclasses)
puts Parent.subclasses.select { |c| c.runnable? }.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment