Module: Eco::Language::Klass::Hierarchy
- Included in:
- AutoLoader, 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>
Note:
in native ruby, you might want #subclasses method.
Finds all child classes of the current class.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/eco/language/klass/hierarchy.rb', line 9 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.
31 32 33 34 35 36 |
# File 'lib/eco/language/klass/hierarchy.rb', line 31 def descendants?(parent_class: self, direct: false) descendants( parent_class: parent_class, direct: direct ).length.positive? end |