Hi list,
I’m trying to find all the classes/modules that descend from a
particular
class/module. Please tell me there’s a better way than this:
Hi list,
I’m trying to find all the classes/modules that descend from a
particular
class/module. Please tell me there’s a better way than this:
the following are code in 6560’s gists · GitHub
class Module
def subclasses
classes = []
ObjectSpace.each_object do |klass|
next unless Module === klass
classes << klass if self > klass
end
classes
end
end
I think the above code is good! but considering the class and module are
referenced by constants in ruby, use Module.constants is more naturally!
the following are mine!
the key is to get rid of the real constant such as PI and so on.
def find_children p_klass
Module.constants.find_all{|c_klass| c_klass!=c_klass.upcase ? p_klass
(Object.const_get c_klass) : nil}
end
class MyString < String
end
puts find_children( String) #=> MyString
require ‘benchmark’
include Benchmark
puts find_children(Benchmark).include?(Tms) #=> fase
puts Tms.name #=> Benchmark::Tms
puts find_children(Benchmark).include?(“Tms”) #=> true
def subclasses
@subclasses ||= []
@subclasses.inject( [] ) do |list, subclass|
list.push(subclass, *subclass.subclasses)
end
end
end
}
That’s probably more efficient than my version, but won’t it fail to
capture
relationships of any pre-existing classes? Seems it will only affect
classes
defined after it has been loaded.
On Aug 21, 2008, at 10:27 AM, James C. wrote:
That’s probably more efficient than my version, but won’t it fail to
capture
relationships of any pre-existing classes? Seems it will only affect
classes
defined after it has been loaded.
that’s correct. generally that’s what people are after.
On Aug 21, 2008, at 7:55 AM, James C. wrote:
Hi list,
I’m trying to find all the classes/modules that descend from a
particular
class/module. Please tell me there’s a better way than this:–
James C.
just let ruby track it for you:
cfp:~ > cat a.rb
class A
end
class B < A
end
class C < A
end
class D < B
end
p D => D.subclasses
p C => C.subclasses
p B => B.subclasses
p A => A.subclasses
p Object => Object.subclasses
BEGIN {
class Class
def inherited other
super if defined? super
ensure
( @subclasses ||= [] ).push(other).uniq!
end
def subclasses
@subclasses ||= []
@subclasses.inject( [] ) do |list, subclass|
list.push(subclass, *subclass.subclasses)
end
end
end
}
cfp:~ > ruby a.rb
{D=>[]}
{C=>[]}
{B=>[D]}
{A=>[B, D, C]}
{Object=>[A, B, D, C]}
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs