Sha256: 2c812cc5b2b23c855aa1f2fded53243138993e9f87d91c8aa3c2c51e2cc3630b

Contents?: true

Size: 1.06 KB

Versions: 7

Compression:

Stored size: 1.06 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) {}

    def descendants # :nodoc:
      descendants = []
      ObjectSpace.each_object(singleton_class) do |k|
        descendants.unshift k unless k == self
      end
      descendants
    end
  rescue StandardError # JRuby 9.0.4.0 and earlier
    def descendants # :nodoc:
      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+.
  #
  #   Integer.subclasses # => [Fixnum, Bignum]
  #
  #   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

7 entries across 7 versions & 1 rubygems

Version Path
activesupport-5.0.0.racecar1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.0.0.rc1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.0.0.beta4 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.0.0.beta3 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.0.0.beta2 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.0.0.beta1.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-5.0.0.beta1 lib/active_support/core_ext/class/subclasses.rb