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.


  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



Discover More Insights: Explore Our Recommended Posts!