Any?
Published 4 October 07 by Justin French, 1 comments
Behold! Ruby weirdness. So I was hanging out in our utterly messy views, and thought to myself “what I want is the opposite of Array#empty?, and I’d call it Array#any?”.
So I fire up the console and have a play.
>> [].any?
=> false
>> [1,2].any?
=> true
Fantastic! But wait, Xavier shows me the dark side of any?:
>> [false].any?
=> false
Gah, so any? returns true if any of the values in the Array are true, not if there’s something (anything) in the Array, as I’d hoped. I guess someone, at some point, had a reason for that.
Of course !my_array.empty? is the answer, but I like even better answers.
Before you go…
Here’s some links to my most popular posts: