rubyguides

Reference

Hash Methods

Methods available on Ruby Hash objects.

  1. Hash#all?

    Check if every key-value pair in a Ruby Hash satisfies a condition. Returns true for empty hashes (vacuous truth).

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Hash#default

    Get or set the default value returned for missing keys in a Ruby hash, or configure a proc for dynamic defaults.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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.

  14. Hash#except

    Return a new hash with specified keys excluded, useful for filtering sensitive data before passing hashes to other methods.

  15. 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.

  16. 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.

  17. 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.

  18. 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.

  19. 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.

  20. Hash#flat_map

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

  21. 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.

  22. 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.

  23. 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.

  24. 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.

  25. 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.

  26. 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.

  27. 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.

  28. 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.

  29. 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.

  30. Hash#merge

    Merge multiple hashes into a new hash, combining key-value pairs with conflict resolution for configuration, defaults, and layered settings.

  31. Hash#merge!

    Merge one or more hashes into the receiver in place, keep the same object, and optionally resolve key conflicts with a block.

  32. 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.

  33. Hash#none?

    Check if no key-value pair in a Ruby Hash satisfies a condition. Returns true for empty hashes (vacuous truth).

  34. Hash#rassoc

    Search a hash by value and return the matching key-value pair. The counterpart to Hash#assoc, which searches by key.

  35. 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.

  36. Hash#select

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

  37. 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.

  38. 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.

  39. 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.

  40. 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.

  41. 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.

  42. Hash#transform_values

    Transform hash values in Ruby while keeping the original keys, with examples for data cleanup, conversions, and in-place updates.

  43. 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.

  44. 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.

  45. 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.

  46. 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.

  47. 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.