You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.adoc
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4690,6 +4690,31 @@ ary[..42]
4690
4690
ary[0..42]
4691
4691
----
4692
4692
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
+
4693
4718
== Numbers
4694
4719
4695
4720
=== Underscores in Numerics [[underscores-in-numerics]]
0 commit comments