class

OrderedHash(K, V)

Inherits Reference < Object

A class that acts both like a hash and an array, where the order of key insertion is remembered.

require "ordered_hash"

shash = OrderedHash(String, String).new
shash["foo"] = "bar"
shash["baz"] = "qux"

shash["foo"]            # ==> "bar"
shash["baz"]?           # ==> "qux"
shash["lemur"]?         # ==> Nil

shash[0]                # ==> "bar"
shash.size              # ==> 2
shash.keys              # ==> ["foo", "baz"]
shash.values            # ==> ["bar", "qux"]

Constants

VERSION = "0.1.0"

Constructors

new(key_value_list : Array(Tuple(K, V)))

Create a OrderedHash from an array of Tuples ({K, V})

Source
new(hash : Hash(K, V))

Create a OrderedHash from a Hash(K, V); the initial order will be somewhat random

Source
new(initial_capacity : Int)

Create an empty OrderedHash with a specified initial capacity

Source
new(pull : JSON::PullParser)

Reads a OrderedHash 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 JSON::ParseException is raised.

Values are parsed using the regular new(pull : JSON::PullParser) method.

Source
new(key_values : Array(NamedTuple(key: K, value: V)))

Create a OrderedHash from an array of NamedTuples ({key: K, value: V})

Source
new

Create an empty OrderedHash

Source
new(*, keys : Array(K), values : Array(V))

Create a OrderedHash from an Array(K) of keys and corresponding Array(V) of values of exactly equal length

Source
new(*, keys : Array(K), &block : K -> V)

Create a OrderedHash from an Array(K) of keys, calling a block for each value

Source

Instance methods

[](key : K) : V

Get the value for the given key, or raise a KeyError if no value is set for that key

Source
[]=(key : K, value : V) : V

Set the value at key; adds key to the end of the ordered list if it does not already exist in the OrderedHash

Source
[]=(key_index : Int32, value : K) : V

Set the value at index key_index; raises IndexError if the key_index is out-of-bounds

Source
[]?(key : K) : V | Nil

Get the value for the given key, or nil if no value has been set for that key

Source
delete(key : K) : V | Nil

Delete the given key, returning its prior value, or raising KeyError if it doesn't exist

Source
each

Iterate each pair

Source
each_key

Iterate each key

Source
each_value

Iterate each value

Source
hash(hasher)

See Object#hash(hasher)

Source
keys

Return an Array(K) of the keys in order

Source
size

The number of entries in the OrderedHash

Source
sort

Create a new OrderedHash with the keys ordered via Array(K).sort

Source
sort!

Sort the keys in place via Array(K).sort

Source
to_json(json : JSON::Builder) : Nil

Serializes this OrderedHash into JSON.

Keys are serialized by invoking to_json_object_key on them. Values are serialized with the usual to_json(json : JSON::Builder) method.

Source
to_s(io)

Output a string representation similar to Hash#to_s(io)

Source
values

Return an Array(V) of the values in order

Source