Sha256: ead6c2d946730853f9cde27656f89f1607af0badc41456e42cdf2009bbae48d3

Contents?: true

Size: 738 Bytes

Versions: 6

Compression:

Stored size: 738 Bytes

Contents

require 'facets/class/subclasses'

class Class

  # List all descedents of this class.
  #
  #   class A ; end
  #   class B < A; end
  #   class C < A; end
  #   A.descendants  #=> [B,C]
  #
  # You may also limit the generational distance the subclass may be from
  # the parent class.
  #
  #   class X ; end
  #   class Y < X; end
  #   class Z < Y; end
  #   X.descendants    #=> [Y,Z]
  #   X.descendants(1) #=> [Y]
  #
  # NOTE: This is a intensive operation. Do not expect it to be very fast.

  def descendants(generations=-1)
    descendants = []
    subclasses.each do |k|
      descendants << k
      if generations != 1
        descendants.concat(k.descendants(generations - 1))
      end
    end
    descendants
  end

end

Version data entries

6 entries across 5 versions & 1 rubygems

Version Path
facets-2.9.2 lib/core/facets/class/descendants.rb
facets-2.9.2 src/core/facets/class/descendants.rb
facets-2.9.1 lib/core/facets/class/descendants.rb
facets-2.9.0 lib/core/facets/class/descendants.rb
facets-2.9.0.pre.2 lib/core/facets/class/descendants.rb
facets-2.9.0.pre.1 lib/core/facets/class/descendants.rb