What I Learned
[nil].any? # => false
Because my expectation was it to be true
. My conception of Enumerable#any?
was that it returns true if it has any record. Not only that, my metal shortcut it was that was an antonym to Array#empty?
.
And why exactly is this interesting?
- Ruby has many methods which mean and do the same thing. Like
Array#size
andArray#length
and, to some extent,Enumerable#count
. - Ruby has many methods which mean and do opposite things. Like
Enumerable#all?
andEnumerable#none?
- Knowing what
empty?
does and of 1. and 2., I believed thatany?
would do its opposite.
Enlightenment came about during code review where I was suggesting to remove the unnecessary if
conditional
if collection.any?
collection.map do |i|
…
After discussing the problem and poking the code we were able to remove the if
by preventing nil
from getting into collection
.
Top comments (0)