OrderedHash(K, V)
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
Constructors
Create a OrderedHash from an array of Tuples ({K, V})
Create a OrderedHash from a Hash(K, V); the initial order will be somewhat random
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.
Create a OrderedHash from an array of NamedTuples ({key: K, value: V})
Create a OrderedHash from an Array(K) of keys and corresponding Array(V) of values of exactly equal length
Instance methods
Get the value for the given key, or raise a KeyError if no value is set for that key
Set the value at key; adds key to the end of the ordered list if it does not already exist in the OrderedHash
Set the value at index key_index; raises IndexError if the key_index is out-of-bounds
Get the value for the given key, or nil if no value has been set for that key
Delete the given key, returning its prior value, or raising KeyError if it doesn't exist
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.