rubyguides

Hash#value?

hash.value?(value) -> true or false

The value? method (alias: has_value?) checks whether a map contains a specific value. It searches all values and returns true if any match.

Basic Usage

# Check if value exists
hash = { a: 1, b: 2, c: 3 }
hash.value?(2)    # => true
hash.value?(99)   # => false

Calling value? against a map you already have in hand gives you a quick boolean that slots neatly into conditionals. The method scans every entry so you do not need to know which key holds the value you are looking for, just the value itself.

Practical Examples

Configuration Check

config = { env: "production", debug: false, port: 8080 }

config.value?("production")  # => true
config.value?(8080)           # => true
config.value?(true)          # => false

Configuration maps often mix strings, numbers, and booleans, and value? treats each type separately. A numeric 8080 is not the same as the string "8080", so the method surfaces type differences that a loose comparison might hide. Checking for an enum-like value like "production" is usually the cleanest use of the method.

Status Checking

statuses = { user1: "active", user2: "inactive", user3: "active" }
statuses.value?("active")   # => true
statuses.value?("pending")  # => false

When status values come from a known set, value? is a fast membership test without the ceremony of extracting and comparing keys. It works well for quick triage logic where the question is “does anyone have this state right now?” rather than “who has it?”

Environment Variables

env_vars = ENV.to_h
env_vars.value?("ruby")  # true if PATH contains ruby

Environment hashes can be large, and scanning them with value? is a convenient way to test a single condition against every variable at once. For repeated checks against the same environment, however, building a reverse lookup or caching the result will save work.

Find the key for a value

# Use select to find key for value
hash = { a: 1, b: 2, c: 3 }
hash.key(2)  # => :b (key for value 2)

# Or iterate
hash.each { |k, v| puts k if v == 2 }  # => b

Once value? confirms that a value exists, you might still need to find the corresponding key. The key method on Hash can reverse-lookup the first key for a given value, and manual iteration works when you need every matching key rather than just the first.

Return Values

# Returns boolean
result = {x: "hello"}.value?("hello")
puts result  # => true

result = {x: "hello"}.value?("world")
puts result  # => false

The return type is always a plain boolean, which means value? fits directly into if and unless guards. There is no special nil or sentinel return; the method returns true only when the value is present in the collection.

Aliases

hash = { a: 1 }

# Both work identically
hash.value?(1)      # => true
hash.has_value?(1)  # => true

The has_value? alias exists purely for naming preference. Both forms call the same implementation, so choose the one that reads more naturally in context. Neither variant modifies the original collection, and there is no performance difference between them since they share the underlying C implementation.

Performance

# value? iterates through all values - O(n)
# For frequent lookups, invert the hash
h = { a: 1, b: 2, c: 3 }
value_to_key = h.invert
value_to_key[2]  # => :b

Use Cases

  • Checking if map contains specific value
  • Configuration validation
  • Status checking
  • Finding if any user has a role
  • Membership testing for map values

searching by value

value? is useful when the value itself is the important part of the lookup and the key does not matter yet. That shows up in configuration checks, feature flags, and any small decision where you only need to know whether a certain value exists anywhere in the collection. The method makes that question very direct and keeps the surrounding code from wandering through the collection manually.

If the lookup becomes frequent or the data grows large, it can be worth reshaping the structure so the search is cheaper. For a one-off check, though, value? keeps the intent plain and the code short. It says exactly what it is doing: scan the values and tell me whether the answer is present.

keeping the question focused on values

Using value? can make the lookup read more like a sentence than a search routine. That is useful when the value is the part that matters and the key is just a storage detail. It also keeps the surrounding code from building temporary arrays or inventing a custom loop for a one-line question. For a quick membership test, that directness is often the clearest option.

when the value is the real lookup target

If a map is standing in for a small registry, value? can be the most natural way to ask whether a setting, role, or status already exists. The method keeps the question close to the business meaning instead of making the reader think about how the data is stored. That is often the right tradeoff for compact code that needs a quick yes-or-no answer and nothing more.

See Also