Averiguando los Descendientes de una Clase (descendants)

Véase:

[~/TheRubyProgrammingLanguageDrive/Chapter7ClassesAndModules]$ cat descendantsofaclass.rb 
require 'pp'

class Parent
  def self.descendants
    ObjectSpace.each_object(Class).select { |klass| klass < self }
  end
end

class Child < Parent
end

class GrandChild < Child
end

pp Parent.descendants # [GrandChild, Child]
pp Child.descendants  # [GrandChild]

[~/TheRubyProgrammingLanguageDrive/Chapter7ClassesAndModules]$ ruby descendantsofaclass.rb 
[GrandChild, Child]
[GrandChild]

Casiano Rodriguez León 2015-01-07