Created
August 27, 2014 18:51
-
-
Save Yardboy/05bb7f5044d7db2bbf5a to your computer and use it in GitHub Desktop.
Ruby any? and all? with an empty array as receiver.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2.0.0-p451 :001 > [].any? { |wtf| wtf.locked? } | |
=> false | |
2.0.0-p451 :002 > [].all? { |wtf| wtf.locked? } | |
=> true |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess it's in the reading of the functionality of #all?
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?".