We often land on a situation where we want to know all the classes which have been inherited from a given class; One such scenario can be while using Single Table Inheritance in Rails.
Here, we might want to identify those classes that are derived from a class.
Rails ~> 3.0 already has such a feature provided
To cater to such requirements in Ruby,
Ruby 3.1 introduced
Class#descendants that returns
an array of classes
directly or indirectly inheriting from the receiver, without
including the receiver or singleton classes.
We can implement
Class#descendants using the code
snippet given below-
For more discussion related to this change, please refer to this PR.