Sha256: 3e27ec33a2a829438b7f37ce53a9cc7aadd96e413f8ccc01f3c039f493c0aed7

Contents?: true

Size: 1.27 KB

Versions: 48

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

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

48 entries across 48 versions & 7 rubygems

Version Path
activesupport-5.2.8.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.8 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.7.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.7 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.6.3 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.6.2 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.6.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.6 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.4.6 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.5 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.4.5 lib/active_support/core_ext/class/subclasses.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.1/lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.4.4 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.4.3 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.4.2 lib/active_support/core_ext/class/subclasses.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.4.1/lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.4.1 lib/active_support/core_ext/class/subclasses.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.4 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.2.4.rc1 lib/active_support/core_ext/class/subclasses.rb