rubyguides

Hash#any?

hash.any? { |key, value| block } → true or false

Hash#any? returns true if at least one key-value pair in the hash satisfies a given condition. It comes from the Enumerable module, which Hash includes. Call it with a block, a pattern argument, or neither.

Syntax

hash.any?                            # → true or false
hash.any?(pattern)                   # → true or false
hash.any? { |key, value| ... }       # → true or false

Parameters

ParameterTypeRequiredDescription
patternObjectNoMatched against each [key, value] pair using ===
blockProcNoReceives key and value separately; truthy return counts as a match

Return Value

Always returns true or false.

How Each Form Works

Block form

The block receives two arguments: the key and value for each pair. If the block returns a truthy value for any pair, any? returns true immediately (short-circuit evaluation — it stops as soon as it finds a match).

scores = { alice: 95, bob: 72, carol: 88 }

scores.any? { |name, score| score < 80 }
# => true  (bob: 72 < 80)

scores.any? { |name, score| score > 100 }
# => false

You can inspect keys, values, or both inside the block:

config = { host: "localhost", port: 8080, ssl: false }

config.any? { |key, value| key == :ssl }
# => true

config.any? { |key, value| value == true }
# => false

No-argument form

Without a block, any? returns true if the hash contains at least one truthy value. Only false and nil are falsy in Ruby.

{ a: 1, b: 2 }.any?
# => true

{ a: false, b: nil }.any?
# => false

{}.any?
# => false

An empty hash always returns false.

Pattern form

With a pattern argument, any? checks whether pattern === element is true for any element. For a Hash, each element is a [key, value] array, so the pattern is matched against those pairs.

inventory = { apples: 10, bananas: 0, cherries: 50 }

# Check if any exact pair matches
inventory.any?([:apples, 10])
# => true

inventory.any?([:grapes, 5])
# => false

# Array matches any pair (all pairs are two-element arrays)
inventory.any?(Array)
# => true

The pattern form works best when you have a complete pair to look for. For value-type checks, use a block instead (see Gotchas below).

Common Use Cases

Checking for a value condition

def any_admin?(users)
  users.any? { |_name, role| role == :admin }
end

users = { alice: :viewer, bob: :admin, carol: :viewer }
any_admin?(users)
# => true

Detecting blank values in a form hash

form = { username: "alice", email: "", password: "secret" }

has_blank = form.any? { |_key, val| val.to_s.strip.empty? }
# => true  (email is blank)

Conditional logic based on hash content

settings = { retries: 0, timeout: nil, verbose: false }

if settings.any? { |_k, v| v }
  puts "At least one setting is active"
else
  puts "All settings are off or unset"
end
# => "All settings are off or unset"

Gotchas

Don’t use any? to check key existence. Use Hash#key? or Hash#has_key? instead — they’re faster and make the intent clear:

config = { debug: false, timeout: 30 }

# Avoid: misleading and slower
config.any? { |k, _v| k == :debug }
# => true

# Prefer: direct and fast
config.key?(:debug)
# => true

The no-argument form checks for truthy values, not for key presence. { a: nil }.any? returns false because the value nil is falsy, even though the key :a is there.

The pattern form checks entire [key, value] pairs, not individual values. Passing a class like Integer tests whether the pair array itself is an Integer — which it never is. Use a block for type checks on values:

data = { count: 5, label: "items" }

# Wrong: checks if [key, value] is an Integer (always false)
data.any?(Integer)
# => false

# Right: checks if any value is an Integer
data.any? { |_k, v| v.is_a?(Integer) }
# => true

See Also