63. あるクラスのサブクラスを取
得する
class Object
def subclasses_of(*superclasses)
subclasses = []
superclasses.each do |sup|
ObjectSpace.each_object(class << sup; self; end) do |k|
if k != sup &&
(k.name.blank? ||
eval(quot;defined?(::#{k}) &&
::#{k}.object_id == k.object_idquot;))
subclasses << k
end
end
end
subclasses
end
end
62/67
64. 使うときは
class Fizz; end
class Buzz < Fizz; end
class FizzBuzz < Fizz; end
subclasses_of(Fizz) #=> [Buzz, FizzBuzz]
63/67