Active Support 后代跟踪器
此模块提供了一个内部实现来跟踪后代,它比遍历 ObjectSpace
更快。
但是,Ruby 3.1 提供了一个快速的原生 +Class#subclasses+ 方法,因此,如果您知道您的代码不会在旧版本的 Ruby 上执行,包括 ActiveSupport::DescendantsTracker
不会带来任何好处。
方法
类公共方法
descendants(klass) 链接
源代码: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/descendants_tracker.rb, line 104 def descendants(klass) klass.descendants end
subclasses(klass) 链接
源代码: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/descendants_tracker.rb, line 100 def subclasses(klass) klass.subclasses end
实例公共方法
descendants() 链接
源代码: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/descendants_tracker.rb, line 109 def descendants subclasses = DescendantsTracker.reject!(self.subclasses) subclasses.concat(subclasses.flat_map(&:descendants)) end
subclasses() 链接
源代码: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/descendants_tracker.rb, line 175 def subclasses DescendantsTracker.subclasses(self) end