Class: Class

Inherits:
Object show all
Defined in:
lib/kitchensink/patches/class.rb,
lib/kitchensink/patches/class.rb

Instance Method Summary collapse

Instance Method Details

#descendantsObject

Return a list of all classes currently known to inherit from this class, directly or indirectly.

NOTE that this only checks objects currently in memory. For instance, if you start a Rails console and check the descendants of ActiveRecord::Base, you’ll only find those objects that have been dynamically loaded.



6
7
8
9
10
11
12
# File 'lib/kitchensink/patches/class.rb', line 6

def descendants
  dd = []
  ObjectSpace::each_object(Class) do |c|
    dd << c if c.ancestors.include?(self)
  end
  dd
end

#immediate_descendantsObject

Return a list of all classes currently known to directly inherit from this class.

NOTE that this only checks objects currently in memory. For instance, if you start a Rails console and check the descendants of ActiveRecord::Base, you’ll only find those objects that have been dynamically loaded.



21
22
23
24
25
26
27
# File 'lib/kitchensink/patches/class.rb', line 21

def immediate_descendants
  dd = []
  ObjectSpace::each_object(Class) do |c|
    dd << c if c.ancestors.size > 1 && c.ancestors[1] == self
  end
  dd
end