Sha256: ebf296bae57b0aaffbd96d6748c3604ac744adbbbef8b2c6a9d4e682c182cd18

Contents?: true

Size: 952 Bytes

Versions: 6

Compression:

Stored size: 952 Bytes

Contents

require 'core_ext/module/anonymous'
require 'core_ext/module/reachable'

class Class
  begin
    ObjectSpace.each_object(Class.new) {}

    def descendants # :nodoc:
      descendants = []
      ObjectSpace.each_object(singleton_class) do |k|
        descendants.unshift k unless k == self
      end
      descendants
    end
  rescue StandardError # JRuby
    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

6 entries across 6 versions & 1 rubygems

Version Path
core_ext-0.0.6 lib/core_ext/class/subclasses.rb
core_ext-0.0.5 lib/core_ext/class/subclasses.rb
core_ext-0.0.4 lib/core_ext/class/subclasses.rb
core_ext-0.0.3 lib/core_ext/class/subclasses.rb
core_ext-0.0.2 lib/core_ext/class/subclasses.rb
core_ext-0.0.1 lib/core_ext/class/subclasses.rb