- Notifications
You must be signed in to change notification settings - Fork162
Document.descendants and.subclasses advice#356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Document.descendants and.subclasses advice#356
Uh oh!
There was an error while loading.Please reload this page.
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Pull Request Overview
This PR adds guidance to the styleguide discouraging the use ofClass#descendants due to reliability issues. The addition documents thatClass#descendants is non-deterministic because it doesn't account for classes that haven't been autoloaded yet and can behave inconsistently with garbage collection, particularly in tests with dynamically defined classes.
- Documents the unreliability of
Class#descendantswith autoloading and garbage collection - Provides a code example demonstrating the problematic pattern
Tip: Customize your code reviews with copilot-instructions.md.Create the file orlearn how to get started.
STYLEGUIDE.md Outdated
| Avoid using`Class#descendants` as it is unreliable. | ||
| It doesn't know about classes that haven't been autoloaded yet, and it's non-deterministic with regards to garbage collection of classes. | ||
| In tests that dynamically define classes, GC timing can mean`Class#descendants` may or may not include those dynamically defined classes. |
CopilotAIOct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
[nitpick] The documentation should provide an alternative approach or workaround for scenarios where developers might need to track subclasses. Without suggesting alternatives, developers may be unclear on what to use instead ofClass#descendants.
0740e10 to0c4848bCompareClass.descendants advice.descendants and.subclasses advice
Uh oh!
There was an error while loading.Please reload this page.
A wise man, let's call him Ben,once said:
And I totally trust him on that because he's making a… GoodJob 🥁
This is generally unwanted to be documented in Rails.
But I also understand Ben's reasoning.
Now the question is: do you want to have it here?
Shall I add the cops (1,2) here? 🤔
PS: I'm looking for a new adventure in case anybody is looking to hire or work with a Ruby/Rails/Crystal dev