Pardon the mess, Play My Code is in beta!

READY TO PLAY?
CLICK TO LOG IN!

sign up - lost password

Hashes

Hashes are similar to arrays in that they store a mapping from a value to an object. In an array the index must be an integer; in a hash the index can be any object.

A Hash is an instance of the Hash class, and so has methods. You can find these listed in the documentation for the Hash class.

Hashes are defined using curly braces, and can contain mappings on creation.

emptyHash = {}

countries = {
        :UK     : new Country('UK'),
        :USA    : new Country('USA'),
        :France : new Country('France')
}

// a hash using different types of keys
people = {
        42         : foo,
        92         : bar,
        "a string" : foobar
}

Mapping a key to a value is achieved using a colon to denote the mapping (like in JavaScript and Ruby 1.9).

Setting Values

Setting and getting values for Hashes, uses the same square bracket syntax as used for Arrays.

unit = {}
unit[ :type ] = 'Archer'
unit[ :hp ] = 38
unit[ :attack ] = 93

Reading Values

Reading values can also be done using square brackets, and if the value is not found, then null is returned.

unit = {
        :type => 'Archer',
        :hp => 38,
        :attack => 93
}

att = unit[ :attack ] // set to 93
name = unit[ :name ] // set to null

See Also