Created
June 10, 2009 18:11
-
-
Save tjstankus/127393 to your computer and use it in GitHub Desktop.
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
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