Hash#none?
hash.none? { |key, value| block } → true or false Hash#none? returns true if no key-value pair in the hash satisfies a given condition. It is the logical inverse of Hash#all? — where all? requires every pair to match, none? requires that none of them do.
Like all?, it is inherited from Enumerable. It has three forms: a block, a pattern argument, and a no-argument form.
Syntax
hash.none? # → true or false
hash.none?(pattern) # → true or false
hash.none? { |key, value| ... } # → true or false
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
pattern | Object | No | Matched against each [key, value] pair using === |
block | Proc | No | Receives key and value; truthy return means the pair matches |
Return Value
Always returns true or false. Short-circuits on the first truthy block result — it stops as soon as any pair satisfies the condition.
How Each Form Works
Block form
The block receives two arguments: the key and value for each pair. If the block returns false or nil for all pairs, none? returns true. If any pair makes the block return truthy, none? returns false immediately.
scores = { alice: 45, bob: 62, carol: 38 }
scores.none? { |name, score| score > 80 }
# => true (no score above 80)
scores.none? { |name, score| score > 40 }
# => false (alice and bob both above 40)
Checking keys:
config = { debug: false, verbose: false, trace: false }
config.none? { |key, value| key.to_s.include?("debug") }
# => false (debug key is present, even though its value is false)
No-argument form
Without a block, none? returns true if no value in the hash is truthy. Falsy values are false and nil.
{ a: nil, b: nil }.none?
# => true (all values are nil, which is falsy)
{ a: false, b: false }.none?
# => true (all values are false)
{ a: nil, b: 1 }.none?
# => false (1 is truthy)
{}.none?
# => true
An empty hash always returns true. There are no pairs to satisfy any condition, so the “no pair matches” condition holds vacuously.
Pattern form
With a pattern argument, none? returns true if pattern === element is false for every element. For a Hash, each element is a [key, value] array, so the pattern is matched against those pairs.
inventory = { apples: 0, bananas: 0, cherries: 0 }
# No pair matches the exact pair
inventory.none?([:apples, 0])
# => false (the apples pair matches)
# No pair is a String
inventory.none?(String)
# => false (all pairs are Arrays, which are not Strings)
# No pair is an Integer
inventory.none?(Integer)
# => true (pairs are Arrays, not Integers)
The pattern form is rarely used with Hash because it matches entire [key, value] pairs, not individual keys or values.
Common Use Cases
Guard clauses
none? reads naturally in guard clauses where you want to skip processing if all entries share a property:
def validate_optional_config(config)
return if config.none? { |k, v| v.nil? }
raise ArgumentError, "Required keys are missing"
end
full_config = { host: "localhost", port: 8080 }
validate_optional_config(full_config) # none? is true → returns early, no error
partial_config = { host: "localhost", port: nil }
validate_optional_config(partial_config) # none? is false → raises
Checking all values are falsy
Check that a hash of feature flags has everything disabled:
flags = { logging: false, analytics: false, debug_mode: false }
if flags.none?
puts "All features disabled — production-safe"
end
Checking no duplicates exist
tags = { post_1: ["ruby", "rails"], post_2: ["python", "ruby"] }
duplicate_exists = tags.none? { |post, tag_list| tag_list.include?("ruby") }
# => false (ruby appears in post_1)
Gotchas
Empty hash returns true. {}.none? { |k, v| something } returns true even though the block never runs. If you need different behaviour for an empty hash:
{}.none? { |k, v| k == :missing }
# => true (vacuous truth — nothing matches)
# If you want false for an empty hash:
hash.empty? ? false : hash.none? { |k, v| condition }
Short-circuit evaluation. none? stops on the first matching pair. If you need to evaluate all pairs (for side effects), use a for loop or Hash#each_value instead:
# This may not log every entry due to short-circuiting
hash.none? do |k, v|
puts "checking #{k}" # may never run for some keys
some_condition(v)
end
No-argument form is not a presence check. { a: nil }.none? returns false because nil is truthy-returning in the sense that nil is falsy, but the no-argument none? returns false when any value is truthy. It is not a way to check key presence. Use hash.key?(:a) for that.
Pattern form matches entire pairs. A class pattern like Symbol tests whether each [key, value] array is a Symbol — it never is. Use a block for key-type checks:
data = { count: 5, label: "items" }
# Wrong: checks if pair is a Symbol (always false for Hash)
data.none?(Symbol)
# => true (pairs are Arrays)
# Right: checks if no value is a Symbol
data.none? { |_k, v| v.is_a?(Symbol) }
# => true (no value is a Symbol)
See Also
- Hash#any? — returns
trueif at least one pair matches - Hash#all? — returns
trueif every pair matches - Enumerable#none? — the Enumerable module version of this method