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.

Options

What is this?

portrait of Justin

This is the online home of Justin French, a designer & web application developer located in Melbourne, Australia. I like finding ways to make things work better. I like clarifying and simplifying. I like to understand how you understand things.

» read more

Subscribe to my feed

Follow me on Twitter

@justinfrench

More Notebook Articles

Show more notebook articles

Search