Hash#any?

Added in v1.8.7 · Updated March 13, 2026 · Hash Methods
ruby hash methods

Hash#any?, Hash#all?, and Hash#none? check whether entries in a hash satisfy a condition. These methods come from the Enumerable module, which Hash includes. They return boolean values and work with both keys and values.

Syntax

hash.any?                          # true if hash has any truthy value
hash.any? { |(key, value)| block } # true if block returns true for any entry
hash.any?(pattern)                 # true if any key matches (Ruby 2.5+)

hash.all?                          # true for empty hashes
hash.all? { |(key, value)| block } # true if block returns true for all entries

hash.none?                         # true if hash has no truthy values
hash.none? { |(key, value)| block } # true if block returns false/nil for all entries

Parameters

ParameterTypeDefaultDescription
patternObjectnilOptional pattern to match keys against (Ruby 2.5+). Can be a Class, Regexp, or Range
blockProcnilOptional block that receives each key-value pair as [key, value]

Examples

Without a block

When called without a block, these methods check for the existence of truthy values:

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

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

{}.any?
# => false

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

{ a: 1 }.all?
# => true

An empty hash returns false for any?, but true for all? and none? because there are no entries to contradict the condition.

With a block

The block receives each key-value pair as a two-element array:

user = { name: Alice, age: 30, email: alice@example.com }

# Check if any value is a string
user.any? { |(_key, value)| value.is_a?(String) }
# => true

# Check if all values are present (not nil)
user.all? { |(_key, value)| !value.nil? }
# => true

# Check if no key starts with a specific letter
user.none? { |(key, _value)| key.to_s.start_with?(z) }
# => true

Checking keys only

You can also pattern-match against keys using the optional argument (Ruby 2.5+):

config = { host: localhost, port: 8080, ssl: true }

config.any?(Symbol)
# => true

config.any?(String)
# => false

config.all?(Integer)
# => false (Symbol keys don't match)

Practical use cases

These methods are useful for validation:

form_data = { username: john, email: john@example.com, age: 25 }

# Validate all fields are present
form_data.all? { |(_k, v)| v&.to_s&.strip&.any? }
# => true

# Check if any field is empty
form_data.any? { |(_k, v)| v.nil? || v.to_s.strip.empty? }
# => false

# Verify no validation errors exist
errors = {}
errors.none?
# => true

Behavior with empty hashes

Ruby follows vacuous truth logic for empty collections:

{}.any?
# => false

{}.all?
# => true

{}.none?
# => true

This makes sense: an empty hash has no entries that could be false, so all entries pass and no entries fail the condition.

Comparison

MethodBlock returns true forResult
any?At least one entrytrue if any match
all?Every entrytrue if all match (or hash empty)
none?No entriestrue if none match (or hash empty)

Edge Cases

Truthy vs falsey values

{ a: false, b: nil }.any?
# => false (both are falsey)

{ a: false, b: nil }.all?
# => false (not all are truthy)

{ a: false, b: nil }.none?
# => true (none are truthy)

Symbol vs string keys

{ :a => 1 }.any? { |(k, _v)| k == :a }
# => true

{ a => 1 }.any? { |(k, _v)| k == :a }
# => false

See Also