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
- Enumerable({K, V})
- Iterable({K, V})
Defined in:
nason/any.crnason/to_json.cr
Constructors
-
.new(pull : NASON::PullParser)
Reads a Hash from the given pull parser.
Instance Method Summary
- #==(other : NASON::Any)
-
#to_nason(json : NASON::Builder) : Nil
Serializes this Hash into NASON.
Instance methods inherited from class Reference
==(other : NASON::Any)
==
Instance methods inherited from class Object
===(other : NASON::Any)
===,
nil_or_null?
nil_or_null?,
not_null!
not_null!,
null?
null?,
to_nason(io : IO) : Nilto_nason : String to_nason, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil to_pretty_json
Class methods inherited from class Object
from_nason(string_or_io, root : String)from_nason(string_or_io) from_nason
Constructor Detail
Reads a Hash from the given pull parser.
Keys are read by invoking from_json_object_key?
on this hash's
key type (K
), which must return a value of type K
or nil
.
If nil
is returned a NASON::ParseException
is raised.
Values are parsed using the regular .new(pull : NASON::PullParser)
method.
Instance Method Detail
Serializes this Hash into NASON.
Keys are serialized by invoking to_json_object_key
on them.
Values are serialized with the usual #to_nason(json : NASON::Builder)
method.