How to find all the subclasses of a class?

prettyfied:

require 'enumerator'
class Module
  def subclasses
    ObjectSpace.to_enum(:each_object, Module).select do |m|
      m.ancestors.include?(self)
    end
  end
end

cheers

Simon

···

-----Original Message-----
From: Sean O'Halpin [mailto:sean.ohalpin@gmail.com]
Sent: Monday, October 24, 2005 1:49 PM
To: ruby-talk ML
Subject: Re: How to find all the subclasses of a class?

Incorporating Robert's suggestions, this is a bit better:

class Module
  def subclasses
    classes =
    ObjectSpace.each_object(Module) do |m|
      classes << m if m.ancestors.include? self
    end
    classes
  end
end

Regards,

Sean

Thanks for all the answers, this solution made my day....

Horacio

Monday 24 October 2005 21:30、Kroeger, Simon (ext) さんは書きました:

···

m.ancestors.include?(self)