Module: Eco::Language::Klass::Hierarchy
- Included in:
- HelpersBuilt
- Defined in:
- lib/eco/language/klass/hierarchy.rb
Instance Method Summary collapse
-
#descendants(parent_class: self, direct: false, scope: nil) ⇒ Arrary<Class>
Finds all child classes of the current class.
-
#descendants?(parent_class: self, direct: false) ⇒ Boolean
trueif the current class has child classes, andfalseotherwise.
Instance Method Details
#descendants(parent_class: self, direct: false, scope: nil) ⇒ Arrary<Class>
Finds all child classes of the current class.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/eco/language/klass/hierarchy.rb', line 8 def descendants(parent_class: self, direct: false, scope: nil) scope ||= ObjectSpace.each_object(::Class) return [] if scope.empty? scope.select do |klass| klass < parent_class end.sort do |k_1, k_2| next -1 if k_2 < k_1 next 1 if k_1 < k_2 0 end.tap do |siblings| next unless direct siblings.reject! do |si| siblings.any? {|s| si < s} end end end |
#descendants?(parent_class: self, direct: false) ⇒ Boolean
Returns true if the current class has child classes, and false otherwise.
30 31 32 |
# File 'lib/eco/language/klass/hierarchy.rb', line 30 def descendants?(parent_class: self, direct: false) descendants(parent_class: parent_class, direct: direct).length.positive? end |