Active Support Descendants Tracker
此模組提供內部實作來追蹤繼承類別,比透過 ObjectSpace
進行反覆運算快。
但 Ruby 3.1 提供了快速的原生 +Class#subclasses+ 方法,所以如果您知道您的程式碼不會在較舊的 Ruby 中執行,則包含 ActiveSupport::DescendantsTracker
沒有任何好處。
方法
類別公開方法
descendants(klass) 連結
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/descendants_tracker.rb, line 102 def descendants(klass) klass.descendants end
subclasses(klass) 連結
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/descendants_tracker.rb, line 98 def subclasses(klass) klass.subclasses end
實例公開方法
descendants() 連結
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/descendants_tracker.rb, line 107 def descendants subclasses = DescendantsTracker.reject!(self.subclasses) subclasses.concat(subclasses.flat_map(&:descendants)) end