class Hash(K, V)

Overview

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.

Included Modules

Defined in:

message_pack/to_msgpack.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Object

===(other : MessagePack::Any) ===, to_msgpack(io : IO)
to_msgpack(packer : MessagePack::Packer)
to_msgpack
to_msgpack

Class methods inherited from class Object

from_msgpack(string_or_io, zero_copy = false) from_msgpack

Constructor Detail

def self.new(pull : MessagePack::Unpacker, block : Hash(K, V), K -> V | Nil = nil) #

[View source]
def self.new(pull : MessagePack::Unpacker, default_value : V) #

[View source]

Class Method Detail

def self.from_msgpack(string_or_io, default_value, zero_copy = false) #

[View source]
def self.from_msgpack(string_or_io, zero_copy = false, &block : Hash(K, V), K -> V) #

[View source]

Instance Method Detail

def to_msgpack(packer : MessagePack::Packer) #

[View source]