blob: bbd8f5aef6b7ed605f69c88cd125a00609d56b6e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
require 'active_support/core_ext/module/anonymous'
require 'active_support/core_ext/module/reachable'
class Class #:nodoc:
# Returns an array with the names of the subclasses of +self+ as strings.
#
# Integer.subclasses # => ["Bignum", "Fixnum"]
def subclasses
Class.subclasses_of(self).map { |o| o.to_s }
end
# Rubinius
if defined?(Class.__subclasses__)
def descendents
subclasses = []
__subclasses__.each {|k| subclasses << k; subclasses.concat k.descendents }
subclasses
end
else
# MRI
begin
ObjectSpace.each_object(Class.new) {}
def descendents
subclasses = []
ObjectSpace.each_object(class << self; self; end) do |k|
subclasses << k unless k == self
end
subclasses
end
# JRuby
rescue StandardError
def descendents
subclasses = []
ObjectSpace.each_object(Class) do |k|
subclasses << k if k < self
end
subclasses.uniq!
subclasses
end
end
end
# Exclude this class unless it's a subclass of our supers and is defined.
# We check defined? in case we find a removed class that has yet to be
# garbage collected. This also fails for anonymous classes -- please
# submit a patch if you have a workaround.
def self.subclasses_of(*superclasses) #:nodoc:
subclasses = []
superclasses.each do |klass|
subclasses.concat klass.descendents.select {|k| k.anonymous? || k.reachable?}
end
subclasses
end
end
|