Véase:
each_object([module]) {|obj| ... }
Calls the block once for each living, nonimmediate object in this Ruby process.
If module is specified, calls the block for only those classes or modules that match (or are a subclass of) module.
Returns the number of objects found. Immediate objects
(Fixnums
, Symbols
true
, false
, and nil
) are never returned.
[~/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