Sha256: 53e5e883dc44eec4df2522b96e415a8cc14cac357bbdaec2d93e782c047f476a

Contents?: true

Size: 738 Bytes

Versions: 44

Compression:

Stored size: 738 Bytes

Contents

# frozen_string_literal: true

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

  # 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
end

Version data entries

44 entries across 44 versions & 7 rubygems

Version Path
activesupport-6.1.7.10 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7.9 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7.8 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7.7 lib/active_support/core_ext/class/subclasses.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7.6 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7.5 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7.4 lib/active_support/core_ext/class/subclasses.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7.3 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7.2 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.7 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.6.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.6 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.5.1 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.5 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.4.7 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.4.6 lib/active_support/core_ext/class/subclasses.rb
activesupport-6.1.4.5 lib/active_support/core_ext/class/subclasses.rb