← Reference

Hash Methods

Methods available on Ruby Hash objects.

Hash#any?

Check if any, all, or no hash entries satisfy a condition

Hash#dig

Safely retrieve nested values from hashes and arrays without raising errors when keys are missing.

hash.dig(*keys) -> value or nil

Hash#each

Iterate over hash key-value pairs with each, each_pair, or each_with_object in Ruby.

hash.each { |key, value| block } -> hash

Hash#each_pair

Iterate over hash key-value pairs with each_pair in Ruby, an alias for each.

hash.each_pair { |key, value| block } -> hash

Hash#fetch

Returns a value from the hash for the given key, with support for default values and blocks.

Hash#filter

Returns a new hash containing only the key-value pairs for which the block returns a truthy value.

hash.filter { |key, value| block } -> hash

Hash#flat_map

Transform hash key-value pairs into arrays. map returns an array of block results; flat_map flattens nested arrays automatically.

hash.map { |key, value| block } -> array

Hash#has_key?

Check if a hash contains a specific key

Hash#has_value?

Check if a hash contains a specific value

Hash#invert

Returns a new Ruby hash with keys and values swapped. Duplicate values cause the last key to win.

Hash#keys

Returns an array containing all the keys in a hash.

hash.keys

Hash#merge

Merge multiple hashes into a new hash, combining key-value pairs with conflict resolution.

hash.merge(*others) -> hash

Hash#reject

Returns a new hash excluding the key-value pairs for which the block returns a truthy value. Use reject! for in-place removal.

hash.reject { |key, value| block } -> hash

Hash#select

Returns a new hash containing only the key-value pairs for which the block returns a truthy value. Alias for filter.

hash.select { |key, value| block } -> hash

Hash#to_a

Converts a hash to an array of key-value pairs using to_a in Ruby.

hash.to_a

Hash#transform_keys

Transform hash keys using a block with transform_keys in Ruby. Available since Ruby 2.5.

hash.transform_keys { |key| block } -> hash

Hash#transform_values

Transform hash values using a block with transform_values in Ruby. Available since Ruby 2.5.

hash.transform_values { |value| block } -> hash

Hash#value?

Returns true if any value in the hash equals the given object, searching hash values.

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

Hash#values

Returns a new array containing all the values in a hash.

hash.values