Skip to content

Commit

Permalink
Class.__subclasses__ was removed from Rubinius.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jan 18, 2011
1 parent 199d1ab commit aa8efc4
Showing 1 changed file with 20 additions and 34 deletions.
54 changes: 20 additions & 34 deletions activesupport/lib/active_support/core_ext/class/subclasses.rb
Expand Up @@ -2,49 +2,35 @@
require 'active_support/core_ext/module/reachable'

class Class #:nodoc:
# Rubinius
if defined?(Class.__subclasses__)
alias :subclasses :__subclasses__
begin
ObjectSpace.each_object(Class.new) {}

def descendants
descendants = []
__subclasses__.each do |k|
descendants << k
descendants.concat k.descendants
ObjectSpace.each_object(class << self; self; end) do |k|
descendants.unshift k unless k == self
end
descendants
end
else # MRI
begin
ObjectSpace.each_object(Class.new) {}

def descendants
descendants = []
ObjectSpace.each_object(class << self; self; end) do |k|
descendants.unshift k unless k == self
end
descendants
end
rescue StandardError # JRuby
def descendants
descendants = []
ObjectSpace.each_object(Class) do |k|
descendants.unshift k if k < self
end
descendants.uniq!
descendants
rescue StandardError # JRuby
def descendants
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 # => [Bignum, Fixnum]
def subclasses
subclasses, chain = [], descendants
chain.each do |k|
subclasses << k unless chain.any? { |c| c > k }
end
subclasses
# 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
end

0 comments on commit aa8efc4

Please sign in to comment.