Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method · added in Ruby 4.1

Module#descendants

descendants → array

Returns an array of classes and modules that have the receiver in their ancestors. This is the inverse of Module#ancestors: +x.descendants.include?(y)+ holds if and only if +y.ancestors.include?(x)+ holds, except that the receiver itself, singleton classes, and refinements are never included. The order of the returned array is not defined.

Example
module A; end
module B; include A; end
class C; include B; end
class D < C; end

A.descendants    #=> [B, C, D]
B.descendants    #=> [C, D]
C.descendants    #=> [D]

Note that the receiver does not hold references to its descendants and doesn’t prevent them from being garbage collected. This means that a descendant might disappear from the result when all references to it are dropped, depending on whether garbage collector was run.

Ruby version changes

  1. 4.1