Skip to content

Commit 0740e10

Browse files
committed
Document Class.descendants advice
1 parent ed60d2b commit 0740e10

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

STYLEGUIDE.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,4 +1096,22 @@ If you need to call a subview that expects an instance variable be set. If possi
10961096

10971097
Unfortunately the only way to get data into a layout template is with instance variables. You can't explicitly pass locals to them.
10981098

1099+
1100+
### Avoid `Class#descendants`
1101+
1102+
Avoid using `Class#descendants` as it is unreliable.
1103+
It doesn't know about classes that haven't been autoloaded yet, and it's non-deterministic with regards to garbage collection of classes.
1104+
In tests that dynamically define classes, GC timing can mean `Class#descendants` may or may not include those dynamically defined classes.
1105+
1106+
```ruby
1107+
# bad
1108+
class Person < ApplicationRecord
1109+
end
1110+
1111+
class Employee < Person
1112+
end
1113+
1114+
Person.descendants # => Unreliable, may or may not include Employee
1115+
```
1116+
10991117
[rubocop-guide]: https://github.com/rubocop-hq/ruby-style-guide

0 commit comments

Comments
 (0)