aboutsummaryrefslogblamecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/class/subclasses.rb
blob: b0f9a8be34363b42c81d50fa5769e3305636f26f (plain) (tree)
1
2
3
4
5
6
7
8
9

                                                  
 
           
       

                                                                    
 
                             
                      
                                                     
                                              

                 
       
                                                  
                             


                                           
         

                       
       
     
 

                                                        



                                              
                          
   
                               



                                                     
       
              

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

class Class
  begin
    # Test if this Ruby supports each_object against singleton_class
    ObjectSpace.each_object(Numeric.singleton_class) {}

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