But it only works after you define a new inheritance. Is there a way to
find descendants of a class that is already in place? For example:
Numeric.descendants
=> [Integer, Float, Complex]
Thank you.
irb(main):001:0> ObjectSpace.each_object(Class) {|cl| p cl if cl < Numeric}
Bignum
Float
Fixnum
Integer
But keep in mind: it's inefficient (so cache if you can), and it may not be standard across rubies (jruby may not have it IIRC). It's useful for interactive poking around though.
···
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
Let me first state that I believe this is intentional: while sub classes need knowledge of their parent classes the opposite is not true. In fact, that would create a kind of circular dependency.
Having said that, this is what you could do
require 'set'
h = Hash.new {|h,k| h[k] = Set.new}
ObjectSpace.each_object(Class) do |cl|
cl.superclass.ancestors.each {|scl| h[scl] << cl}
end
p h[String]
You should keep in mind though that this is by no means static. New classes can come in existence at any time.
Kind regards
robert
···
On 12.08.2009 21:30, Jason Lillywhite wrote:
Ruby can do Class#ancestors but not Class#descendants.
ObjectSpace.each_object(Class) do |cl|
cl.superclass.ancestors.each {|scl| h[scl] << cl}
end
I'm getting this error:
irb(main):085:0> ObjectSpace.each_object(Class) do |cl|
irb(main):086:1* cl.superclass.ancestors.each {|scl| h[scl] << cl}
irb(main):087:1> end
NoMethodError: undefined method `ancestors' for nil:NilClass
from (irb):86
from (irb):85:in `each_object'
from (irb):85
Is there a reason superclass is returning nil on cl?
This is actually the one each_object case we do support with general
ObjectSpace support turned off, since we maintain a weak reference
from parent to child classes (so we just walk everything from Object
down; it's for our method-caching logic).
- Charlie
···
On Wed, Aug 12, 2009 at 2:39 PM, Joel VanderWerf<vjoel@path.berkeley.edu> wrote:
irb(main):001:0> ObjectSpace.each_object(Class) {|cl| p cl if cl < Numeric}
Bignum
Float
Fixnum
Integer
But keep in mind: it's inefficient (so cache if you can), and it may not be
standard across rubies (jruby may not have it IIRC). It's useful for
interactive poking around though.
Oops, I inserted "superclass" after doing the test. This is likely the case of Object. If you remove it it works as well. Sorry, for the hassle.
Kind regards
robert
···
On 12.08.2009 21:57, Jason Lillywhite wrote:
Robert Klemme wrote:
ObjectSpace.each_object(Class) do |cl|
cl.superclass.ancestors.each {|scl| h[scl] << cl}
end
I'm getting this error:
irb(main):085:0> ObjectSpace.each_object(Class) do |cl|
irb(main):086:1* cl.superclass.ancestors.each {|scl| h[scl] << cl}
irb(main):087:1> end
NoMethodError: undefined method `ancestors' for nil:NilClass
from (irb):86
from (irb):85:in `each_object'
from (irb):85
from :0
Is there a reason superclass is returning nil on cl?
It doesn't filter out the "included wrapper" from included modules,
but it's there. I suggested it as an official addition to Ruby, but
nobody seemed interested in the idea at the time.
- Charlie
···
On Thu, Sep 3, 2009 at 12:16 AM, Charles Oliver Nutter<headius@headius.com> wrote:
This is actually the one each_object case we do support with general
ObjectSpace support turned off, since we maintain a weak reference
from parent to child classes (so we just walk everything from Object
down; it's for our method-caching logic).
It doesn't filter out the "included wrapper" from included modules,
but it's there. I suggested it as an official addition to Ruby, but
nobody seemed interested in the idea at the time.