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

ruby-subclasses

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.

Recommended Posts

Rails 5 adds OR Activerecord method
This or method was introduced in Rails 5, It returns a new ActiveRecord::Relation which is...
Ruby 3.1 adds subclasses method
Ruby 3.1 adds Class#subclasses method, which returns an array of subclasses directly inheriting from the...
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...