Enumerable#any?
What does any? do?
any? returns true if at least one element in the collection satisfies the given condition. If a block is provided, it tests each element against the block’s condition. If no block is given, it checks whether any element is truthy.
The method short-circuits — it stops iterating as soon as it finds a matching element, making it efficient for large collections.
Basic Usage
With a block
[1, 2, 3].any? { |n| n > 2 } # => true
[1, 2, 3].any? { |n| n > 5 } # => false
Without a block
When called without a block, any? checks for truthy values. Only nil and false are falsy — everything else is truthy:
[1, 2, 3].any? # => true
[false, nil, 1].any? # => true
[false, nil].any? # => false
[].any? # => false
This makes any? useful for checking whether a collection has any non-empty, non-nil content.
With a Predicate Argument
You can pass a pattern object as an argument. The method treats it like a block that calls === on the pattern:
[1, 2, 3, 4].any?(Integer) # => true
[1, 2, 3, 4].any?(String) # => false
["a", "b", "c"].any?(/b/) # => true
["a", "b", "c"].any?(/x/) # => false
This is equivalent to:
[1, 2, 3, 4].any? { |n| Integer === n } # => true
Practical Examples
Checking if any number meets a condition
temperatures = [12, 15, 8, 22, 19]
if temperatures.any? { |t| t > 30 }
puts "Warning: High temperature detected!"
else
puts "All temperatures within safe range"
end
# => All temperatures within safe range
Checking for a matching string
users = ["alice", "bob", "charlie"]
users.any? { |name| name.start_with?("a") } # => true
users.any? { |name| name.start_with?("z") } # => false
Checking if any elements exist
def process_queue(queue)
return unless queue.any?
# process items...
end
process_queue([]) # => nil (early return)
process_queue([1]) # => continues
Validating input
def has_required_role?(user_roles, required_role)
user_roles.any? { |role| role == required_role }
end
has_required_role?([:read, :write], :admin) # => false
has_required_role?([:read, :admin], :admin) # => true
Short-Circuit Evaluation
any? stops as soon as it finds a truthy result. This matters when the block has side effects or when performance is critical:
# Stops after finding the first match
[1, 2, 3, 4].any? do |n|
puts "Checking #{n}" # This prints only for 1 and 2
n > 1
end
# => true
# Output:
# Checking 1
# Checking 2
Empty collections
any? always returns false for an empty collection, regardless of the block:
[].any? { |x| x > 5 } # => false
[].any? # => false
Performance Considerations
Lazy evaluation
Because any? short-circuits, the worst case (no element matches) requires checking every element, but the best case (first element matches) exits immediately:
# Worst case: O(n)
[1, 2, 3].any? { |n| n > 100 } # => false, checks all 3
# Best case: O(1)
[100, 200, 300].any? { |n| n > 50 } # => true, stops at first element
Arrays vs Sets
For membership testing, any? over an array performs linear search. If you frequently check whether any element matches a value, consider a Hash or Set:
require "set"
tags = Set.new(["ruby", "rails", "web"])
# Array: O(n) linear search each time
languages = ["ruby", "python", "go"]
languages.any? { |lang| lang == "ruby" } # => true
# Set: O(1) lookup
tags.any? { |tag| tag == "ruby" } # => true
Relationship to all? and none?
any? is the complement of none? and the opposite of all?:
| Method | Returns true when… |
|---|---|
any? | At least one element satisfies the condition |
all? | Every element satisfies the condition |
none? | No element satisfies the condition |
# any? vs all?
[1, 2, 3].any? { |n| n > 2 } # => true (3 > 2)
[1, 2, 3].all? { |n| n > 2 } # => false (1 > 2 and 2 > 2 are false)
# any? vs none?
[1, 2, 3].any? { |n| n > 5 } # => false
[1, 2, 3].none? { |n| n > 5 } # => true (no element is > 5)
You can often choose between any? and none? based on readability:
# These are equivalent:
collection.any? { |x| x.nil? }
collection.none? { |x| !x.nil? }
See Also
- /reference/enumerable/enumerable-all/ — Returns true if all elements satisfy the condition
- /reference/enumerable/enumerable-none/ — Returns true if no element satisfies the condition
- /reference/enumerable/enumerable-find/ — Returns the first element that satisfies a condition