Ruby 3.1 introduces the
Class#descendants method which returns all descendants
of a class excluding the receiver and singleton classes.
We can see many implementations for calculating all descendant classes of a
particular class from the Ruby community with different gems. The
is one of such implementations used in Rails framework. Finally, Ruby has added
Class#descendants native implementation for it’s 3.1 version release.
1=> class User; end 2=> class Employee < User; end 3=> class Client < User; end 4 5=> class Manager < Employee; end 6=> class Developer < Employee; end 7 8=> User.descendants 9=> [Employee, Client, Manager, Developer] 10 11=> Employee.descendants 12=> [Manager, Developer]