Sha256: 4002cc6a24c0ca7c2b3fde6b0a9a80db382c1e877b920efcb385f636c285fdc7

Contents?: true

Size: 1.34 KB

Versions: 23

Compression:

Stored size: 1.34 KB

Contents

require "active_support/core_ext/module/anonymous"
require "active_support/core_ext/module/reachable"

class Class
  begin
    # Test if this Ruby supports each_object against singleton_class
    ObjectSpace.each_object(Numeric.singleton_class) {}

    # 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
      descendants = []
      ObjectSpace.each_object(singleton_class) do |k|
        next if k.singleton_class?
        descendants.unshift k unless k == self
      end
      descendants
    end
  rescue StandardError # JRuby 9.0.4.0 and earlier
    def descendants
      descendants = []
      ObjectSpace.each_object(Class) do |k|
        descendants.unshift k if k < self
      end
      descendants.uniq!
      descendants
    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
    subclasses, chain = [], descendants
    chain.each do |k|
      subclasses << k unless chain.any? { |c| c > k }
    end
    subclasses
  end
end

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
activesupport-5.1.7 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.7.rc1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.6.2 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.6.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.6 lib/active_support/core_ext/class/subclasses.rb
tdiary-5.0.8 vendor/bundle/gems/activesupport-5.1.5/lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.5 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.5.rc1 lib/active_support/core_ext/class/subclasses.rb
pract6-0.1.0 .gem/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.4 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.4.rc1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.3 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.3.rc3 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.3.rc2 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.3.rc1 lib/active_support/core_ext/class/subclasses.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.1.2/lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.2 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.2.rc1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.1.0 lib/active_support/core_ext/class/subclasses.rb