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
by ActiveSupport
Class#descendants.
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.
Before
We can implement Class#descendants
using the code
snippet given below-
After
For more discussion related to this change, please refer to this PR.