Enumerating Ruby’s “Enumerable” Module, Part 1: “all?” and “any?”

by Joey deVilla on January 29, 2008

The raison d’etre

While I often refer to the documentation at Ruby-Doc.org, I often find its descriptions a little unclear, lacking in detail and even missing some vital information. In the “do it yourself and share it afterwards” spirit of Open Source, I’ve decided to start working on a series of articles that I’ll eventually compile into a single site to become a useful, complete and better alternative to Ruby-Doc.org’s docs. These articles will appear here on Global Nerdy on an ongoing basis, and I hope you’ll find them useful.

My plan is to start with a module whose methods you’re guaranteed to use in your day-to-day Ruby development: Enumerable, a mixin that adds traversal, search, filtering and sorting functionality to collection classes, including those workhorses known as Array and Hash. My articles on Enumerable‘s methods should fill in some holes Ruby-Doc.org’s coverage (especially for hashes, which gets surprisingly little coverage). I’ll be going through Enumerable‘s methods alphabetically.

In this installment, I’ll cover two of Enumerable‘s methods: all? and any?

all?

  • In plain language: Do all the items in the collection meet the given criteria?
  • Ruby.Doc.org’s entry: Enumerable#all?
  • Expects: A block containing the criteria (it’s optional, but you’re likely to use one most of the time).
  • Returns:
    • true if all the items in the collection meet the given criteria
    • false otherwise

Using all? with Arrays

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

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

Using all? with Hashes

When used on a hash and a block is provided, all? 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 never returns false or nil during this process, all? returns true; otherwise, it returns false.

Using all? without a block on a hash is meaningless, as it will always return true. When the block is omitted, all? 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 all?, will still return true:

Special Case: Using all? on Empty Arrays and Hashes

When applied to an empty array or hash, with or without a block, all? always returns true.

Let’s look at the case of empty arrays:

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

any?

  • In plain language: Do any of the items in the collection meet the given criteria?
  • Ruby.Doc.org’s entry: Enumerable#any?
  • Expects: A block containing the criteria (it’s optional, but you’re likely to use one most of the time).
  • Returns:
    • true if any of the items in the collection meet the given criteria
    • false otherwise

Using any? with 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).

Using any? with 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 any? on Empty Arrays and Hashes

When applied to an empty array or hash, with or without a block, any? always returns false.

Let’s look at the case of empty arrays:

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

In the Next Installment…

…the collect (a.k.a. map) method.

Previous post:

Next post: