Ruby 3.1 Class#descendants | BigBinary Blog


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
ActiveSupport::DescendantsTracker
is one of such implementations used in Rails framework. Finally, Ruby has added
the 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]

Here’s the relevant pull request and
feature discussion for this change.



Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here