This question is similar / related to another stack overflow question about Looking up all the descendants of a class in Ruby. A wonderful question full of the information that I was looking for - except that when I drop down into a rails console:
irb(main):001:0> ActiveSupport::DescendantsTracker.descendants(Object)
=>[]
irb(main):002:0> ObjectSpace.each_object(Class).select { |klass| klass < Object }
=> [IRB::Notifier::AbstractNotifier, IRB::Notifier::ErrUnrecognizedLevel, ...]
So, why isn't ActiveSupport::DescendantsTracker returning the descendants of Object? What are the differences in the implementation? The documentation for DescendantsTracker suggests that:
This module provides an internal implementation to track descendants which is faster than iterating through ObjectSpace.
Faster? Ok, it's gotta be faster to return nothing versus something (right?), but it's supposed to return the descendants of the supplied class.
ActiveSupport::DescendantsTracker.descendants(Object)
will be returning blank in your console because the development console doesn't compile your application, it hasn't yet loaded all of the classes and therefore doesn't know about them to output them!
Take a look at this question: RoR: MyModel.descendants returns [] in a view after the first call?
You need to eager load the classes, as stated in: https://github.com/rails/rails/issues/3364
ActionDispatch::Reloader.to_prepare do
Rails.application.eager_load!
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With