Check if every key-value pair in a Ruby Hash satisfies a condition. Returns true for empty hashes (vacuous truth).
Reference
Hash Methods
Methods available on Ruby Hash objects.
- Hash#all?
- Hash#any?
Check if any, all, or no hash entries satisfy a condition
- Hash#any?
Check if any key-value pair in a Ruby Hash satisfies a condition. Hash#any? supports a block, pattern, or no-argument form.
- Hash#compact
Remove nil values from a hash with Hash#compact and Hash#compact!.
- Hash#compare_by_identity
Configures a Hash to use object identity instead of eql? for key comparisons.
- Hash#count
Returns the number of key-value pairs in a hash, or the count of entries matching a condition.
- Hash#default
Get or set the default value returned for missing keys in a Ruby hash, or configure a proc for dynamic defaults.
- Hash#delete
Remove a key-value pair from a hash and return the deleted value. Returns nil (or the result of an ifnone block) when the key is not found.
- Hash#dig
Safely retrieve nested values from hashes and arrays without raising errors when keys are missing.
- Hash#dig
Navigate deeply nested hashes and arrays. Use dig to retrieve a value from a chain of keys without raising an error if any intermediate value is nil.
- Hash#each
Iterate over hash key-value pairs with each, each_pair, or each_with_object in Ruby.
- Hash#each_pair
Iterate over hash key-value pairs with each_pair in Ruby, an alias for each.
- Hash#each_with_object
Iterate over a hash, passing each key-value pair and a memo object into the block. The memo object is accumulated across iterations and returned at the end.
- Hash#except
Return a new hash with specified keys excluded, useful for filtering sensitive data before passing hashes to other methods.
- Hash#fetch
Retrieve a value by key, with explicit error handling when the key is absent. Use fetch to avoid silent nil returns from missing keys.
- Hash#fetch
Retrieve a value by key, with explicit error handling when the key is absent. Use fetch to avoid silent nil returns from missing keys.
- Hash#fetch_values
Fetch multiple hash keys at once. Hash#fetch_values returns an array of values and raises KeyError for missing keys unless a block handles them.
- Hash#filter
Returns a new hash containing only the key-value pairs for which the block returns a truthy value.
- Hash#filter_map
Filter and transform hash entries in one pass. Hash#filter_map calls the block for each key-value pair, keeps only the truthy results, and returns an array.
- Hash#flat_map
Transform hash key-value pairs into arrays. map returns an array of block results; flat_map flattens nested arrays automatically.
- Hash#group_by
Group hash entries by a criteria derived from each key-value pair. Returns a hash where keys are group labels and values are arrays of matching entries.
- Hash#has_key?
Check if a hash contains a specific key
- Hash#has_value?
Check if a hash contains a specific value
- Hash#include?
Check if a hash contains a specific key
- Hash#invert
Returns a new Ruby hash with keys and values swapped. Duplicate values cause the last key to win.
- Hash#key?
Check if a hash contains a specific key
- Hash#keys
Returns an array containing all the keys in a hash.
- Hash#max_by
Find the key-value pair in a hash with the largest value, or the top N pairs by a criterion you define.
- Hash#merge
Combine hashes non-destructively. Later hash values override duplicate keys. Block resolves conflicts.
- Hash#merge
Merge multiple hashes into a new hash, combining key-value pairs with conflict resolution.
- Hash#merge!
Merges each given hash into self, modifying it in place and returning self.
- Hash#min_by
Find the key-value pair in a hash that minimizes the value returned by the block. Returns a two-element array of the key and value.
- Hash#none?
Check if no key-value pair in a Ruby Hash satisfies a condition. Returns true for empty hashes (vacuous truth).
- Hash#rassoc
Search a hash by value and return the matching key-value pair. The counterpart to Hash#assoc, which searches by key.
- 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#select
Returns a new hash containing only the key-value pairs for which the block returns a truthy value. Alias for filter.
- Hash#store
Associates a given value with a given key in a hash. Returns the value that was stored, not the hash itself.
- Hash#sum
Sum hash values with a block, or add key-value pairs directly. Works via Enumerable so you get proper key, value block parameters.
- Hash#to_a
Converts a hash to an array of key-value pairs using to_a in Ruby.
- Hash#to_h
Convert a hash to a plain hash, or transform key-value pairs using a block. Essential for working with subclassed hashes and Enumerable results.
- Hash#transform_keys
Transform hash keys using a block with transform_keys in Ruby. Available since Ruby 2.5.
- Hash#transform_values
Transform hash values using a block with transform_values in Ruby. Available since Ruby 2.5.
- Hash#update
Merges each given hash into self, returning self. Alias for Hash#merge!.
- Hash#value?
Check if a hash contains a specific value
- Hash#value?
Returns true if any value in the hash equals the given object, searching hash values.
- Hash#values
Returns a new array containing all the values in a hash.
- Hash#values_at
Return values for multiple keys at once. Hash#values_at takes an array of keys and returns an array of the corresponding values.