> For the complete documentation index, see [llms.txt](https://kelby.gitbook.io/rails-beginner-s-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kelby.gitbook.io/rails-beginner-s-guide/activesupport_other_class_and_module/descendants_tracker.md).

# Descendants Tracker

查看某个类的子类。功能上和 Ruby 内置库 Object Space 类似，但性能上要比它好得多。

```ruby
class A
  extend ActiveSupport::DescendantsTracker
end

class B < A
end

class C < A
end

class D < B
end

# 输出 A 的所有子类
A.descendants
=> [B, D, C]

# 输出 A 的所有直接子类
A.direct_descendants
=> [B, C]
```

手法：重写 inherited 方法，当发生继承关系时，记录到 `@@direct_descendants` 里。
