Sha256: fde237b8b53299499c4b65a28ea44dcc6e2ed907fb59d33e8885f873a290cd6b

Contents?: true

Size: 1019 Bytes

Versions: 41

Compression:

Stored size: 1019 Bytes

Contents

# frozen_string_literal: true

require "active_support/ruby_features"

class Class
  if ActiveSupport::RubyFeatures::CLASS_SUBCLASSES
    # 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
      subclasses.concat(subclasses.flat_map(&:descendants))
    end
  else
    def descendants
      ObjectSpace.each_object(singleton_class).reject do |k|
        k.singleton_class? || k == self
      end
    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
    descendants.select { |descendant| descendant.superclass == self }
  end unless ActiveSupport::RubyFeatures::CLASS_SUBCLASSES
end

Version data entries

41 entries across 38 versions & 9 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.8.7/lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.8.7 lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.8.6 lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.8.5 lib/active_support/core_ext/class/subclasses.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/core_ext/class/subclasses.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/class/subclasses.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/class/subclasses.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.8.4 lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.8.1 lib/active_support/core_ext/class/subclasses.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/core_ext/class/subclasses.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.8 lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.7.2 lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.7.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.7 lib/active_support/core_ext/class/subclasses.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/core_ext/class/subclasses.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/core_ext/class/subclasses.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/core_ext/class/subclasses.rb
activesupport-7.0.6 lib/active_support/core_ext/class/subclasses.rb