(hashes were added in v1.1)
A hash is a variable type that maps one set of values to another, much like an array maps indexes to values. In a hash each pair of values is called a key/value pair, where the key value can be used to lookup the other value. Unlike an array the key value can be of any type, not just an integer.
To create a new hash you use the Hash object type command (see more below), eg:
$favorites = Hash.new('animal', 'Bunny')
To define a key/value pair in a hash you use curly braces to enclose the key:
$favorites{'color'} = 'cerulean'
In this example the hash now maps the key 'color' to the string 'cerulean'. Mixing types in a hash is perfectly valid:
$map{7} = 'Heaven7'
$map{0.5} = '1/2'
$map{'PI'} = 3.14
To retrieve a value from an existing hash you use the same curly brace notation:
$favColor = $favorites{'color'}
If the key is not currently defined by the hash then the value returned is the @undefined value.
Warning: hash key equality comparisons do not make full use of value coercion that would apply when using other kinds of equality tests. It is best to always use the same type of value (eg: text vs float) for a key when setting and getting a value from a hash. An example that illustrates the problem:
$hash{3.14} = 'Pie!' # key is a numeric type
$failed = $hash{"3.14"} # key is now a text type, failure
Value Semantics
By default hashes use value semantics, which means a copy of the hash is made each time it is assigned. This ensures each variable is independent from one another. For example:
$original = Hash.new("a", 123, "z", 999)
$copy = $original # a copy of $original is implicitly made
$copy{"m"} = '😱' # only modifies $copy, leaving $original intact
Prompt $original, $copy # shows two different hashes
If you need to use reference semantics, create a Ref object to the hash.
Note: value semantics are not used when passing hashes as arguments to commands.
Hash Type Commands
Hash.new [key1, value1], [key2, value2] ... [keyN, valueN] v1.1
Returns a new hash object. Each successive two arguments define a key/value pair in that hash.
Hash Object Properties
.count v1.1
The number of key/value pairs the hash defines. This property is read-only.
.keys v1.1
An array that contains all the keys defined by the hash. The order of the returned keys is not defined. This property is read-only.
.values v1.1
An array that contains all the values defined by keys of the hash. The order of the returned values is not defined. This property is read-only.
Hash Object Commands
.valueForKey key v1.1
Returns the value associated with the given hash key. If the key is not defined, returns the @undefined value. This command is equivalent to:
$value = $hash{$key}
.setValueForKey value, key v1.1
Defines the given key/value pair for the hash. If the key is already defined, it is associated with the given value instead of the old value. This command is equivalent to:
$hash{$key} = $value
.removeKey key v1.1
Removes the key from the hash so the hash no longer defines the key.
.definesKey key v1.1
Returns @true if the key is defined, otherwise @false.
Previous Chapter Arrays |
<< index >> |
Next Chapter Primitive Objects |