[ruby-core:126291] [Ruby Feature#9779] Add Module#descendants
Issue #9779 has been updated by matz (Yukihiro Matsumoto). Accepted. Let's define `Module#descendants`. This settles the question I left open in [#14394](https://bugs.ruby-lang.org/issues/14394#note-38). `Class#subclasses` already lets you write the transitive closure for classes in Ruby, but there is no equivalent primitive for modules, so `Module` is where this method actually adds something. Defining it there also gives `Class` the method for free. Not including `self` is fine too. It departs from `ancestors`, but it agrees with `Class#subclasses` and with ActiveSupport's `descendants`, and `[mod, *mod.descendants]` is available when needed. Once this is merged, I think [#14394](https://bugs.ruby-lang.org/issues/14394) can be closed. Matz. ---------------------------------------- Feature #9779: Add Module#descendants https://bugs.ruby-lang.org/issues/9779#change-118388 * Author: shugo (Shugo Maeda) * Status: Open ---------------------------------------- Now classes have links to their subclasses, so how about to add Module#descendents? ```ruby class X end class Y < X end class Z < Y end p X.descendents #=> [X, Y, Z] module A end module B include A end module C include A end p A.descendents #=> [A, C, B] ``` One of my own use cases is to implement AspectJ like pointcut which matches all subclasses of the given class. Considerations: * Should the return value of Module#descendents include self? * Should the return value of Module#descendents include singleton classes? I attach a patch, where the return value includes both self and singleton classes. ---Files-------------------------------- 0001-Add-Module-descendents.patch (3.75 KB) -- https://bugs.ruby-lang.org/
participants (1)
-
matz (Yukihiro Matsumoto)