aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/class/subclasses.rb
diff options
context:
space:
mode:
authorGonçalo Silva <goncalossilva@gmail.com>2010-07-10 17:36:10 +0100
committerGonçalo Silva <goncalossilva@gmail.com>2010-07-10 17:36:10 +0100
commitcd2bbed9846d84a1230a1b9e52843eedca17b28d (patch)
tree5214b7855f3d102e4c22239b9d62bc5717cb3547 /activesupport/lib/active_support/core_ext/class/subclasses.rb
parentd2c633ba0bfb7baacdee89a46d7d036d24c68817 (diff)
parent80e47d7b88dcc732ebeb5290faab6e529829dac6 (diff)
downloadrails-cd2bbed9846d84a1230a1b9e52843eedca17b28d.tar.gz
rails-cd2bbed9846d84a1230a1b9e52843eedca17b28d.tar.bz2
rails-cd2bbed9846d84a1230a1b9e52843eedca17b28d.zip
Merge branch 'master' of http://github.com/rails/rails
Diffstat (limited to 'activesupport/lib/active_support/core_ext/class/subclasses.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/class/subclasses.rb57
1 files changed, 26 insertions, 31 deletions
diff --git a/activesupport/lib/active_support/core_ext/class/subclasses.rb b/activesupport/lib/active_support/core_ext/class/subclasses.rb
index 7d58a8b56a..3e5d1a2a42 100644
--- a/activesupport/lib/active_support/core_ext/class/subclasses.rb
+++ b/activesupport/lib/active_support/core_ext/class/subclasses.rb
@@ -2,54 +2,49 @@ 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__)
+ alias :subclasses :__subclasses__
+
def descendants
- subclasses = []
- __subclasses__.each {|k| subclasses << k; subclasses.concat k.descendants }
- subclasses
+ descendants = []
+ __subclasses__.each do |k|
+ descendants << k
+ descendants.concat k.descendants
+ end
+ descendants
end
- else
- # MRI
+ else # MRI
begin
ObjectSpace.each_object(Class.new) {}
def descendants
- subclasses = []
+ descendants = []
ObjectSpace.each_object(class << self; self; end) do |k|
- subclasses << k unless k == self
+ descendants.unshift k unless k == self
end
- subclasses
+ descendants
end
- # JRuby
- rescue StandardError
+ rescue StandardError # JRuby
def descendants
- subclasses = []
+ descendants = []
ObjectSpace.each_object(Class) do |k|
- subclasses << k if k < self
+ descendants.unshift k if k < self
end
- subclasses.uniq!
- subclasses
+ descendants.uniq!
+ descendants
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.descendants.select {|k| k.anonymous? || k.reachable?}
+ # Returns an array with the direct children of +self+.
+ #
+ # Integer.subclasses # => [Bignum, Fixnum]
+ def subclasses
+ subclasses, chain = [], descendants
+ chain.each do |k|
+ subclasses << k unless chain.any? { |c| c > k }
+ end
+ subclasses
end
- subclasses
end
end