Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. Hash.keys / values / has_key? / has_value?

Hash.keys / values / has_key? / has_value?

Methods for retrieving a list of keys or values from a hash, and for checking whether a specific key or value exists. Frequently used for data inspection and debugging.

Syntax

# Returns an array of all keys.
hash.keys

# Returns an array of all values.
hash.values

# Checks whether a key exists (several aliases available).
hash.has_key?(key)
hash.key?(key)
hash.include?(key)
hash.member?(key)

# Checks whether a value exists.
hash.has_value?(value)
hash.value?(value)

# Returns an array of values for the specified keys.
hash.values_at(key1, key2, ...)

Method list

MethodDescription
keysReturns all keys in the hash as an array. Insertion order is preserved.
valuesReturns all values in the hash as an array. Insertion order is preserved.
has_key?(key)Returns true if the specified key exists. key?, include?, and member? are aliases.
has_value?(val)Returns true if the specified value exists. value? is an alias.
values_at(k1, k2, ...)Returns an array of values for the specified keys. Missing keys produce nil.
key(value)Returns the first key that maps to the specified value. Returns nil if not found.
size / lengthReturns the number of key-value pairs in the hash.
empty?Returns true if the hash contains no entries.

Sample code

person = { name: 'Taro', age: 30, city: 'Tokyo' }

# List keys and values
puts person.keys.inspect    # [:name, :age, :city]
puts person.values.inspect  # ["Taro", 30, "Tokyo"]

# Check for key existence
puts person.has_key?(:name)    # true
puts person.key?(:email)       # false (key not found)
puts person.include?(:age)     # true (alias of has_key?)

# Check for value existence
puts person.has_value?('Taro') # true
puts person.value?(99)         # false

# Retrieve values for multiple keys at once
puts person.values_at(:name, :city).inspect   # ["Taro", "Tokyo"]
puts person.values_at(:name, :email).inspect  # ["Taro", nil] (missing key returns nil)

# Look up a key by value
puts person.key('Taro')    # name
puts person.key('unknown') # nil

# Size and empty check
puts person.size    # 3
puts person.empty?  # false
puts {}.empty?      # true

# Compare hash keys using set intersection
config1 = { host: 'localhost', port: 3000 }
config2 = { host: 'example.com', port: 80, ssl: true }
common_keys = config1.keys & config2.keys
puts common_keys.inspect  # [:host, :port] (keys shared by both hashes)

Notes

Since Ruby 1.9, hashes maintain insertion order, so the arrays returned by keys and values reflect the order in which entries were added.

has_key? has three aliases: key?, include?, and member?. All four behave identically, but the Ruby style guide often recommends the shorter key?. Pick one and use it consistently across your codebase.

values_at is convenient when you need to retrieve several values at once. Because missing keys return nil, check for key existence with has_key? beforehand, or use fetch to supply a default value.

If you find any errors or copyright issues, please .