The Hash data type

A Hash is a generic collection of key-value pairs mapping keys to values. The keys and values can be of any data type including custom classes.

Hashes are typically created with a hash literal denoted by curly braces ( { } ) enclosing a list of pairs using => as the delimiter between the key and the value and separated by commas.

{"name" => "Tony Stark", "rich?" => True}
{} # An empty Hash.

Accessing a value within a hash requires its key:

var person = {"name" => "Tony Stark", "rich?" => True}
person{"name"} # "Tony Stark"

Assigning a value to a key within a Hash is easy enough. If the Hash doesn’t contain the specified key then it’s created. The standard assignment operators are supported:

var h = {"a" => 10, "b" => 20}
h{"c"} = 100 # {"a" => 10, "b" => 20, "c" => 100}
h{"a"} += 50 # {"a" => 60, "b" => 20, "c" => 100}

Comprehensive documentation of the Hash object’s methods and getters can be found in the Hash section.