class

Hash(K, V)

Inherits Iterable < Enumerable < Reference < Object

A Hash represents a collection of key-value mappings, similar to a dictionary.

Main operations are storing a key-value mapping (#[]=) and querying the value associated to a key (#[]). Key-value mappings can also be deleted (#delete). Keys are unique within a hash. When adding a key-value mapping with a key that is already in use, the old value will be forgotten.

# Create a new Hash for mapping String to Int32
hash = Hash(String, Int32).new
hash["one"] = 1
hash["two"] = 2
hash["one"] # => 1

Hash literals can also be used to create a Hash:

{"one" => 1, "two" => 2}

Implementation is based on an open hash table. Two objects refer to the same hash key when their hash value (Object#hash) is identical and both objects are equal to each other (Object#==).

Enumeration follows the order that the corresponding keys were inserted.

NOTE: When using mutable data types as keys, changing the value of a key after it was inserted into the Hash may lead to undefined behaviour. This can be restored by re-indexing the hash with #rehash.

Class methods

merge_r(a, b, block : Proc(KB, VB, VB, VB)) forall KB, VB

Recursive deep-merge worker: block type stays fixed at the OUTERMOST instantiation while inner hashes merge at their own K/V (inner values are members of the outer V union, hence the casts).

Source
stringify_r(h) : Hash(String, ActiveSupport::Any)

Recursive key-type converters (values widen to ActiveSupport::Any — Crystal cannot express recursive hash types).

Source
transform_values_r(h, block : Proc(VB, VB)) forall VB

Recursive value transformer (same pattern as merge_r).

Source

Instance methods

assert_valid_keys(*valid_keys) : self
Source
blank?
Source
compact_blank
Source
deep_dup
Source
deep_merge(other : Hash(K, V)) : Hash(K, V)
Source
deep_merge(other : Hash(K, V), &block : K, V, V -> V) : Hash(K, V)
Source
deep_merge!(other : Hash(K, V)) : self
Source
deep_stringify_keys
Source
deep_symbolize_keys
Source
deep_transform_keys
Source
deep_transform_values
Source
except(*keys) : Hash(K, V)
Source
reverse_merge(other : Hash(K, V)) : Hash(K, V)
Source
reverse_merge!(other : Hash(K, V)) : self
Source
slice(*keys) : Hash(K, V)
Source
stringify_keys
Source
symbolize_keys
Source
with_defaults(other : Hash(K, V)) : Hash(K, V)
Source