-
-
Save Yardboy/05bb7f5044d7db2bbf5a to your computer and use it in GitHub Desktop.
2.0.0-p451 :001 > [].any? { |wtf| wtf.locked? } | |
=> false | |
2.0.0-p451 :002 > [].all? { |wtf| wtf.locked? } | |
=> true |
I guess it's in the reading of the functionality of #all?
Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } which will cause all? to return true when none of the collection members are false or nil.
And I get that it's written that way, and I assume it's written that way for a reason. Seems very counter-intuitive, though - I'm used to Ruby reading very much like English. My expectation for the sentence when I use #all? would be "are all the elements of the array locked?" and the answer would be "no" so I think it should return false. The way it actually works, I guess the sentence is really "Are any of the elements of the array not locked?".
Enumerable#any?
"Does at least one element of the collection fulfill the condition?"
Enumerable#all?
"Do all the elements of the array fulfill the condition?"
With an empty collection, zero elements fulfill the condition. Which isn't at least one, but is all of them.
any?
seems self explanatory to me. There aren't any.all?
will return false as soon as it encounters an element that fails the predicate. Since there is no element to test, it has no element to falsify it, so it must be true!