-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
subclasses.rb
41 lines (38 loc) · 1019 Bytes
/
subclasses.rb
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
# frozen_string_literal: true
require "active_support/ruby_features"
class Class
if ActiveSupport::RubyFeatures::CLASS_SUBCLASSES
# Returns an array with all classes that are < than its receiver.
#
# class C; end
# C.descendants # => []
#
# class B < C; end
# C.descendants # => [B]
#
# class A < B; end
# C.descendants # => [B, A]
#
# class D < C; end
# C.descendants # => [B, A, D]
def descendants
subclasses.concat(subclasses.flat_map(&:descendants))
end
else
def descendants
ObjectSpace.each_object(singleton_class).reject do |k|
k.singleton_class? || k == self
end
end
end
# Returns an array with the direct children of +self+.
#
# class Foo; end
# class Bar < Foo; end
# class Baz < Bar; end
#
# Foo.subclasses # => [Bar]
def subclasses
descendants.select { |descendant| descendant.superclass == self }
end unless ActiveSupport::RubyFeatures::CLASS_SUBCLASSES
end