Enumerating Enumerable: Enumerable#any?

by Joey deVilla on June 24, 2008

Welcome to the second installment of Enumerating Enumerable, my project to do a better job of documenting Ruby’s Enumerable module than RubyDoc.org. This installment will cover Enumerable#any?, which I like to think of as Enumerable.all?‘s more easy-going cousin (I covered Enumerable.all? in the previous installment).

Enumerable#any? Quick Summary

Graphic representing Ruby\'s Enumerable#any? method

In the simplest possible terms Do any of the items in the collection meet the given criteria?
Ruby version 1.8 and 1.9
Expects A block containing the criteria. This block is optional, but you’re likely to use one in most cases.
Returns true if any of the items in the collection meet the given criteria.

false if none of the items in the collection does not meet the given criteria.

RubyDoc.org’s entry Enumerable#any?

Enumerable#any? and Arrays

When used on an array and a block is provided, any? passes each item to the block. If the block returns true for any item during this process, any? returns true; otherwise, it returns false.

When the block is omitted, any? uses this implied block: {|item| item}. Since everything in Ruby evaluates to true except for false and nil, using any? without a block is effectively a test to see if any of the items in the collection evaluate to true (or conversely, if all the values in the array evaluate to false or nil).

Enumerable#any? and Hashes

When used on a hash and a block is provided, any? passes each key/value pair in the hash to the block, which you can “catch” as either:

  1. A two-element array, with the key as element 0 and its corresponding value as element 1, or
  2. Two separate items, with the key as the first item and its corresponding value as the second item.

If the block returns true for any item during this process, any? returns true; otherwise, it returns false.

Using any? without a block on a hash is meaningless, as it will always return true. When the block is omitted, any? uses this implied block: {|item| item}. In the case of a hash, item will always be a two-element array, which means that it will never evaluate as false nor nil.

And yes, even this hash, when run through any?, will still return true:

Special Case: Using Enumerable#any? on Empty Arrays and Hashes

When applied to an empty array or hash, with or without a block, any? always returns false. That’s because with an empty collection, there are no values to process and return a true value.

Let’s look at the case of empty arrays:

…now let’s look at the case of empty hashes:

{ 4 comments… read them below or add one }

1 Denis June 24, 2008 at 9:03 am

Does #any? stop when first item in the collection meet the given criteria?

2 Sami Samhuri June 24, 2008 at 12:58 pm

Denis: Yes, #any? breaks after finding the first suitable element.

3 Denis June 25, 2008 at 4:13 am

Sami Samhuri: thanks, I think it is very important feature. Is this a good idea to use
@collection.any?
instead of
@collection.size > 0
for rails AR collections?

4 Sander June 27, 2008 at 2:25 am

Denis: it’s usually better in Rails to use @collection.empty? to check for empty collections. nil, [], {}, and “” are all ‘empty’. I think Rails 2.1 now has present? as the opposite of empty? (not 100% on the name), which is better than @collection.size > 0 in basically all cases.

Leave a Comment

{ 10 trackbacks }

Previous post:

Next post: