Sha256: f2eb21cc47e0b9d27c5a4aa3a26b8a33e766760e1010cc9adfdb38d36b945a30

Contents?: true

Size: 884 Bytes

Versions: 4

Compression:

Stored size: 884 Bytes

Contents

# frozen_string_literal: true

require "active_support/ruby_features"

class 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
    ObjectSpace.each_object(singleton_class).reject do |k|
      k.singleton_class? || k == self
    end
  end unless ActiveSupport::RubyFeatures::CLASS_SUBCLASSES

  # 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
    descendants.select { |descendant| descendant.superclass == self }
  end unless ActiveSupport::RubyFeatures::CLASS_SUBCLASSES
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activesupport-7.0.0 lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.0.rc3 lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.0.rc2 lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.0.rc1 lib/active_support/core_ext/class/subclasses.rb