Sha256: 229b1d95ff9975d1446969d4f364eab1a26729910247e40f63d7b2036f31fb69

Contents?: true

Size: 1.49 KB

Versions: 10

Compression:

Stored size: 1.49 KB

Contents

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

class Class #:nodoc:
  # Returns an array with the names of the subclasses of +self+ as strings.
  #
  #   Integer.subclasses # => ["Bignum", "Fixnum"]
  def subclasses
    Class.subclasses_of(self).map { |o| o.to_s }
  end

  # Rubinius
  if defined?(Class.__subclasses__)
    def descendents
      subclasses = []
      __subclasses__.each {|k| subclasses << k; subclasses.concat k.descendents }
      subclasses
    end
  else
    # MRI
    begin
      ObjectSpace.each_object(Class.new) {}

      def descendents
        subclasses = []
        ObjectSpace.each_object(class << self; self; end) do |k|
          subclasses << k unless k == self
        end
        subclasses
      end
    # JRuby
    rescue StandardError
      def descendents
        subclasses = []
        ObjectSpace.each_object(Class) do |k|
          subclasses << k if k < self
        end
        subclasses.uniq!
        subclasses
      end
    end
  end

  # Exclude this class unless it's a subclass of our supers and is defined.
  # We check defined? in case we find a removed class that has yet to be
  # garbage collected. This also fails for anonymous classes -- please
  # submit a patch if you have a workaround.
  def self.subclasses_of(*superclasses) #:nodoc:
    subclasses = []
    superclasses.each do |klass|
      subclasses.concat klass.descendents.select {|k| k.anonymous? || k.reachable?}
    end
    subclasses
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
csd-0.1.5 lib/active_support/core_ext/class/subclasses.rb
csd-0.1.4 lib/active_support/core_ext/class/subclasses.rb
csd-0.1.3 lib/active_support/core_ext/class/subclasses.rb
csd-0.1.2 lib/active_support/core_ext/class/subclasses.rb
csd-0.1.1 lib/active_support/core_ext/class/subclasses.rb
csd-0.1.0 lib/active_support/core_ext/class/subclasses.rb
csd-0.0.16 lib/active_support/core_ext/class/subclasses.rb
activesupport-3.0.0.beta4 lib/active_support/core_ext/class/subclasses.rb
activesupport-3.0.0.beta3 lib/active_support/core_ext/class/subclasses.rb
activesupport-3.0.0.beta2 lib/active_support/core_ext/class/subclasses.rb