Ruby 3.1 adds subclasses method


Ruby 3.1 adds Class#subclasses method, which returns an array of subclasses directly inheriting from the receiver, not including singleton classes.

ruby-subclasses

  class Parent; end
  
  class ChildA < Parent; end
  class ChildB < Parent; end
  
  class GrandChildA < ChildA; end

  Parent.subclasses
  => [ChildA, ChildB]

  ChildA.subclasses    
  => [GrandChildA]
  
  GrandChildA.subclasses    
  => []

Reference #Pull Request

Recommended Posts

Rails 5 adds OR Activerecord method
This or method was introduced in Rails 5, It returns a new ActiveRecord::Relation which is...
Descendants vs Subclasses in Ruby
Descendants has been a part of Rails ActiveSupport::DescendantsTracker for quite a while. However, Class#subsclasses is...
How to query records which has missing associations in Rails 6
Rails 6 provides a simpler way to query the records which does not have any...