跳至内容 跳至搜索

Active Support Descendants Tracker

此模块提供了一个内部实现来跟踪后代,这比迭代 ObjectSpace 更快。

然而,Ruby 3.1 提供了快速的原生 +Class#subclasses+ 方法,所以如果你知道你的代码不会在旧版本的 Ruby 上运行,那么包含 ActiveSupport::DescendantsTracker 没有任何好处。

方法
D
S

类公共方法

descendants(klass)

# File activesupport/lib/active_support/descendants_tracker.rb, line 102
def descendants(klass)
  klass.descendants
end

subclasses(klass)

# File activesupport/lib/active_support/descendants_tracker.rb, line 98
def subclasses(klass)
  klass.subclasses
end

实例公共方法

descendants()

# File activesupport/lib/active_support/descendants_tracker.rb, line 107
def descendants
  subclasses = DescendantsTracker.reject!(self.subclasses)
  subclasses.concat(subclasses.flat_map(&:descendants))
end