Contents

Roo

Hash.each()

Iterates through this Hash, calling the passed function for each key-value pair, passing to the function the current key as the first parameter and the corresponding value as the second parameter. The method can take an optional second parameter as an Array object (arguments). The elements of this array will be passed to the function as additional parameters.

hash.each(func as Function, arguments? as Array) as Hash

Parameters

  • func. A reference to a function. This function must have been defined with at least two parameters as the current key and value will be passed to it. This function may have additional parameters defined and these will be passed to it as the contents of the optional arguments array
  • arguments. Optional Array object. Each element of this array will be passed to func as additional parameters after the current key and value

Returns

  • This unaltered Hash

Example

function put(key, value) {
    print(key + " is " + value)
}

function putPrefix(key, value, prefix) {
    print(prefix + key + " is " + value)
}

var h = {"a" => 100, "b" => 200}
h.each(put)
# Prints:
# a is 100
# b is 200

h.each(putPrefix, ["* "])
# Prints:
# * a is 100
# * b is 200