Skip to content

Commit 7a3e2f8

Browse files
committed
Add collection querying rule
1 parent 38a8961 commit 7a3e2f8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

README.adoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4690,6 +4690,31 @@ ary[..42]
46904690
ary[0..42]
46914691
----
46924692

4693+
=== Collection querying [[collection-querying]]
4694+
4695+
When possible, use https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-Methods+for+Querying[predicate methods from `Enumerable`] rather than expressions with `#count`.
4696+
4697+
[source,ruby]
4698+
----
4699+
# bad
4700+
array.count > 0
4701+
array.count(&:something).positive?
4702+
4703+
array.count == 0
4704+
4705+
array.count == 1
4706+
4707+
# good
4708+
array.any?
4709+
array.any?(&:something)
4710+
4711+
array.none?
4712+
4713+
array.one?
4714+
----
4715+
4716+
NOTE: Predicate methods aren't applicable when collection includes falsy values (e.g. `[nil].one?` evaluates to `false`).
4717+
46934718
== Numbers
46944719

46954720
=== Underscores in Numerics [[underscores-in-numerics]]

0 commit comments

Comments
 (0)