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 entries satisfy a condition with Hash#any?, all?, and none?. Boolean queries with Ruby examples for validation and config checks.
- Hash#assoc
Search a hash by key and return the matching key-value pair as a two-element array [key, value]. Returns nil if no match is found.
- Hash#compact
Remove nil values from a hash with Hash#compact and Hash#compact!, while preserving false values and understanding the nil return from the bang form.
- Hash#compare_by_identity
Ruby's Hash#compare_by_identity switches key lookup to object identity, treating each reference as a distinct entry regardless of value equality.
- Hash#count
Use Hash#count to get the number of key-value pairs in a hash, or pass a block to count entries matching a condition. Faster than filters for existence checks.
- 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, or nil when the key is not found, with an optional fallback block.
- Hash#dig
Safely retrieve deeply nested values from hashes and arrays without raising errors when keys are missing, and keep deep lookups compact in Ruby.
- 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, depending on whether you want side effects or want to collect a new result.
- Hash#each_pair
Iterate over hash key-value pairs with each_pair in Ruby, an alias for each that keeps pair-oriented iteration clear and readable.
- Hash#each_with_object
Iterate over a hash with a memo object that accumulates across iterations, making it easy to build arrays, hashes, or summary objects in one pass.
- Hash#except
Return a new hash with specified keys excluded, useful for filtering sensitive data before passing hashes to other methods.
- Hash#fetch
Use Hash#fetch in Ruby to retrieve values by key with explicit error handling. Raise KeyError, return a default, or execute a block when the key is absent.
- Hash#fetch
Retrieve a value from a Ruby hash with explicit missing-key handling, including defaults, fallback blocks, and KeyError behavior when the key is absent.
- 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 hash of key-value pairs where the block is truthy. Use Hash#filter in Ruby to clean data, chain transforms, and select entries by key or 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 whether a hash contains a specific key with Hash#has_key? and its aliases, especially when nil is a valid stored value.
- Hash#has_value?
Check whether a Ruby hash contains a specific value, including nil, and compare it with has_key? when you need value-based lookup.
- Hash#include?
Hash#include? checks whether a given key exists in a Ruby hash, returning true or false. Covers the four aliases, nil key handling, and guard clause patterns.
- Hash#invert
Hash#invert swaps keys and values in a Ruby hash, returning a new hash. Learn how duplicate values affect the result and how to handle them safely.
- Hash#key?
Check if a hash contains a specific key, return true when it exists, and keep lookup logic simple when the value might be nil or false.
- Hash#keys
Learn how to use Hash#keys in Ruby to return an array of all keys from a hash in insertion order, for iteration, set operations, and key inspection tasks.
- Hash#max_by
Use Hash#max_by to find the key-value pair with the largest block return value. Covers top-N retrieval, empty hashes, defaults, and finding all tied maxima.
- Hash#merge
Hash#merge combines hashes non-destructively, returning a new hash where later values override duplicate keys. A block resolves conflicts with custom logic.
- Hash#merge
Merge multiple hashes into a new hash, combining key-value pairs with conflict resolution for configuration, defaults, and layered settings.
- Hash#merge!
Merge one or more hashes into the receiver in place, keep the same object, and optionally resolve key conflicts with a block.
- 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 Ruby hash. Returns the stored value, not the hash itself, so you can inspect the inserted value directly.
- 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
Hash#to_a turns a Ruby hash into a nested array of key-value pairs so you can sort, filter, or partition entries with array methods, then convert back.
- 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
Hash#transform_keys creates a new Ruby hash by applying a block to each key, useful for normalizing string or symbol keys and converting naming conventions.
- Hash#transform_values
Transform hash values in Ruby while keeping the original keys, with examples for data cleanup, conversions, and in-place updates.
- Hash#update
Merges hashes into self with Hash#update, an alias for merge!. Covers block conflict resolution, multiple arguments, and destructive hash merging in Ruby.
- Hash#value?
Hash#value? returns true if a value exists in a Ruby hash, false otherwise. Handles nil and false values correctly for membership tests and validation.
- Hash#value?
Hash#value? returns true if any entry in the hash contains the given object, searching all values for a match. The alias has_value? works identically.
- Hash#values
Extract every value from a Hash as a new array in insertion order. Covers iteration, sorting, summing, and when each_value is the better choice.
- 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.