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:
The block examples above test values against numeric thresholds, but the same pattern works for any condition. Testing keys is equally common — checking whether a particular configuration key is absent, confirming that no entry matches a naming convention, or validating that a collection of flags is completely disabled.
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)
The block form lets you test arbitrary conditions on both keys and values. In the last example, none? returns false because the :debug key exists — the check is about the key name, not its value. The block receives the key and value as separate arguments, so you can destructure them naturally without wrapping in an extra array. The block should return a boolean — truthy means “this pair matches,” and none? stops and returns false on the first truthy result.
When you want to check only keys or only values, the block gives you that flexibility. For key-only checks, ignore the value parameter with an underscore prefix: |key, _value|. For value-only checks, do the same with the key parameter: |_key, value|. The method itself makes no distinction — it simply passes both to your block and relies on your logic.
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 collection always returns true for none? — this is the same vacuous-truth behavior seen in Enumerable#none?. There are no pairs to satisfy any condition, so the “no pair matches” condition holds vacuously. If your code treats an empty collection as a special case, check hash.empty? before calling none?.
Pattern form
With a pattern argument, none? returns true if pattern === element is false for every element. 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:
The pattern form is rarely used with a hash because it matches entire [key, value] pairs, not individual keys or values. A class pattern like Symbol tests whether each pair array is a Symbol — it never is, since pairs are always Array objects. For key-type or value-type checks, write a block instead. The examples above show this limitation clearly: inventory.none?(String) returns false because no pair array is a string, not because no value is a string.
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
The guard clause pattern works well here because none? reads as a natural English question: “are there no nil values?” When the answer is yes, the method returns early; when no, it raises. The block keeps the condition close to the guard, so the intent stays visible without scrolling to a separate validation method. The short-circuit behavior also means the guard stops checking as soon as it finds one nil — which is all it needs to know to decide that validation should proceed.
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
The no-argument form of none? checks whether every value in the collection is falsy — nil or false. This is a concise way to confirm that nothing is active, but it is also a blunt tool: a value of 0, an empty string "", or an empty array [] is truthy and would make none? return false. Use it when the values are genuinely boolean flags, not when they might hold other falsy-like data.
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)
The variable is named duplicate_exists but none? returns false when a match is found, so the result is the logical opposite of what the name suggests. Naming a variable after the method result directly — no_duplicates = ... — would make the following branches easier to read. When the name aligns with the boolean meaning, the surrounding conditional reads like a natural sentence, and the reader does not need to mentally invert the variable to understand the branch.
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:
Short-circuit evaluation. none? stops on the first matching pair. If you need to evaluate all pairs — for side effects like logging or metrics collection — use a for loop or Hash#each_value instead. The short-circuit is an optimization, not a debugging tool, and the early exit can hide partial state from the caller.
# 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