Hash#value?
hash.value?(value) -> true or false Returns:
Boolean · Updated March 13, 2026 · Hash Methods hashes searching values lookup
The value? method (alias: has_value?) checks whether a hash 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
Practical Examples
Configuration Check
config = { env: "production", debug: false, port: 8080 }
config.value?("production") # => true
config.value?(8080) # => true
config.value?(true) # => false
Status Checking
statuses = { user1: "active", user2: "inactive", user3: "active" }
statuses.value?("active") # => true
statuses.value?("pending") # => false
Environment Variables
env_vars = ENV.to_h
env_vars.value?("ruby") # true if PATH contains ruby
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
Return Values
# Returns boolean
result = {x: "hello"}.value?("hello")
puts result # => true
result = {x: "hello"}.value?("world")
puts result # => false
Aliases
hash = { a: 1 }
# Both work identically
hash.value?(1) # => true
hash.has_value?(1) # => true
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 hash contains specific value
- Configuration validation
- Status checking
- Finding if any user has a role
- Membership testing for hash values