Hash#has_value?
The has_value? method checks whether a hash contains a specific value. It’s the value-checking counterpart to has_key?, so it is useful when the thing you care about is the data stored in the hash rather than the key name.
In practice, this method is most useful when the same value might appear under different keys, or when you want to confirm that a particular placeholder has been filled in. It keeps that check readable and avoids manual loops over every value.
Syntax
hash.has_value?(value) # => true or false
hash.value?(value) # => true or false (alias)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | Object | Required | The value to search for in the hash |
Return Value
Returns true if the hash contains the given value, false otherwise.
Description
Ruby’s Hash class provides two methods for checking value existence:
has_value?— The primary method name.value?— A shorter alias that does the same thing.
Both methods search through all hash values and return a boolean. They handle any Ruby object as a value, including nil.
This method is useful when you need to verify that a particular value exists in a hash, regardless of which key it’s associated with.
That can be helpful in validation code, reporting code, or any place where the presence of a value matters more than its location. The alias is there mostly for readability, so you can choose whichever name fits the surrounding code better.
Examples
Basic usage
hash = { name: "Alice", age: 30, city: "London" }
hash.has_value?("Alice") # => true
hash.has_value?(100) # => false
hash.has_value?(30) # => true
hash.has_value?(nil) # => false
The basic examples show that has_value? compares against the values already stored in the hash, not against keys or structure. That makes the method easy to reason about when you are reading a small config or a parsed object. The search is linear, so it checks every entry, but for small or medium-sized hashes the convenience usually outweighs the cost. In practice, you call it to answer a yes-or-no question about the data, and the result is immediately usable in a conditional.
Using the alias
config = { theme: "dark", debug: false }
config.value?("dark") # => true
config.value?(true) # => false
The alias reads nicely when the surrounding code is already speaking in terms of values. It keeps the call short while still making the intent clear to the reader. Choosing between the two names is mostly a matter of what the rest of the method already uses for other hash lookups, but both names share the same linear scan underneath.
Checking for nil values
data = { placeholder: nil, valid: "something" }
data.has_value?(nil) # => true - finds the nil value
data.has_value?("") # => false
Checking for nil is a practical use case because placeholder values often show up in partially built data. The method handles that case directly, so you do not need a special loop or a manual any? check across all entries.
With different value types
mixed = { id: 1, name: "Test", active: true, tags: [:a, :b] }
mixed.has_value?(1) # => true (integer)
mixed.has_value?("Test") # => true (string)
mixed.has_value?(:a) # => false (not the array)
mixed.has_value?([:a, :b]) # => true (array)
Mixed value types are a good reminder that the comparison is about equality, not type categories. If the exact value is present, the method returns true, even when the value is a nested array or a boolean.
Performance Note
This method iterates through all values in the hash, making it O(n) complexity. For large hashes or frequent lookups, this is slow.
If you find yourself calling has_value? repeatedly, consider these alternatives:
- Invert the hash if you need to look up keys by value:
hash.invert - Use a Set for large collections of unique values
- Re-think the data structure — if you’re searching for values often, a hash may not be the right choice
That tradeoff is the main reason has_value? is best for occasional checks, not hot paths. When the lookup is frequent, a structure designed around value search is usually a better fit.
See Also
hash-has-key— For checking keys instead of valueshash-dig— Safely access nested valueshash-fetch— Fetch a value with a default or block