rubyguides

Array#any?

arr.any? { |element| block } -> true or false

any? is an Enumerable method that tests whether any element in a collection matches a given condition. It evaluates each element and returns true if at least one element satisfies the criteria, otherwise false.

Syntax

array.any? { |element| block }

Without a block, it checks for truthy values.

Parameters

ParameterTypeDefaultDescription
blockProcA condition to evaluate against each element

In practice, any? is often the method you reach for when an array feeds a conditional branch. The syntax is tiny, but the idea behind it is important: you are asking whether at least one item matches, not trying to transform the collection itself. That distinction makes the method easy to place in validation and checking code.

Examples

Basic usage

numbers = [1, 2, 3, 4, 5]

numbers.any? { |n| n > 3 }
# => true (because 4 and 5 are greater than 3)

numbers.any? { |n| n > 10 }
# => false

For arrays that feed conditionals, any? is often the quickest checking tool because it answers a question without building extra data. The code stays focused on the condition itself, and the result is easy to branch on in the next line. That makes it a natural fit when the goal is simply to know whether a match exists.

Without a block

[1, 2, 3].any?
# => true (has truthy values)

[false, nil, 0].any?
# => true (0 is truthy!)

[false, nil].any?
# => false

The blockless form only looks at the truthiness of the elements, so it is not the same thing as asking whether an array is empty. That difference matters when values like false, nil, and 0 all appear together. Using the method carefully keeps the meaning of the check clear.

With pattern matching (Ruby 3.0+)

items = [1, "hello", :symbol, 3.14]

items.any?(String)
# => true (has String values)

items.any?(Integer)
# => true (has Integer values)

This style is handy when you want to know whether an array contains a certain class or module without writing a custom block. It keeps the type check close to the data and reads well in code that already works with mixed arrays.

Common Patterns

Validation checks

# Check if any field changed
def any_changed?(attributes)
  attributes.any? { |_, v| v_changed?(v) }
end

That pattern shows up in form objects and change tracking, where the code needs to know whether any field has moved away from its original value. It works well because the block can stay small and direct while still scanning the whole array or hash for a match.

Short-circuit evaluation

any? stops after finding the first truthy result, making it efficient:

# Efficient: stops at first expensive operation
results.any? { |item| process_expensive(item) }

Short-circuit behavior is the reason any? can save time on bigger collections. As soon as one element satisfies the condition, the rest of the array does not need to be checked. That makes the method a good choice when each test carries some cost.

Errors

The method rarely raises exceptions. Note that an empty array returns false:

[].any?
# => false (not nil!)

That return value is useful in boolean checks because it stays consistent with the idea of “no match found.” It also means the method is safe to use inside larger expressions without extra nil handling. The result is a clean yes-or-no answer that fits naturally in Ruby control flow.

Performance Notes

any? iterates lazily and stops as soon as it finds a truthy result, making it extremely efficient for large datasets.

Practical Tips

any? is useful when you want a direct yes-or-no answer without building a temporary array. That makes it a strong fit for validation checks, search-style conditions, and feature toggles where the first match is enough.

The blockless form checks the truthiness of the elements themselves, which is not the same as asking whether the array is empty. Keeping that distinction in mind helps avoid bugs where false, nil, and zero are treated differently than expected.

See Also