Descendants vs Subclasses in Ruby


Descendants has been a part of Rails ActiveSupport::DescendantsTracker for quite a while. However, Class#subsclasses is recently introduced in Ruby 3.1

descendants is available in Rails.

subsclasses is native ruby method and is available in ruby 3.1

# available in rails
  Parent.descendants
  => [ChildA, GrandChildA, ChildB]

# available in ruby
  Parent.subclasses
  => [ChildA, ChildB]

Notice: descendants return all the child classes and subclasses only returns child classes directly associated to parent class.



Discover More Insights: Explore Our Recommended Posts!