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 hash entries satisfy a condition

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

  4. Hash#compact

    Remove nil values from a hash with Hash#compact and Hash#compact!.

  5. Hash#compare_by_identity

    Configures a Hash to use object identity instead of eql? for key comparisons.

  6. Hash#count

    Returns the number of key-value pairs in a hash, or the count of entries matching a condition.

  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. Returns nil (or the result of an ifnone block) when the key is not found.

  9. Hash#dig

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

  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.

  12. Hash#each_pair

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

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

  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

    Retrieve a value by key, with explicit error handling when the key is absent. Use fetch to avoid silent nil returns from missing keys.

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

  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 new hash containing only the key-value pairs for which the block returns a truthy 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 if a hash contains a specific key

  23. Hash#has_value?

    Check if a hash contains a specific value

  24. Hash#include?

    Check if a hash contains a specific key

  25. Hash#invert

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

  26. Hash#key?

    Check if a hash contains a specific key

  27. Hash#keys

    Returns an array containing all the keys in a hash.

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

  29. Hash#merge

    Combine hashes non-destructively. Later hash values override duplicate keys. Block resolves conflicts.

  30. Hash#merge

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

  31. Hash#merge!

    Merges each given hash into self, modifying it in place and returning self.

  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 hash. Returns the value that was stored, not the hash itself.

  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

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

  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

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

  42. Hash#transform_values

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

  43. Hash#update

    Merges each given hash into self, returning self. Alias for Hash#merge!.

  44. Hash#value?

    Check if a hash contains a specific value

  45. Hash#value?

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

  46. Hash#values

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

  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.