prev - up - next - index

# Hash

  The class for associative arrays or hash tables. The hash table can contain the association from any kind of object to any kind of object. The hash table allocation can be done by the hash expression:

  >     {a=>b,...}

  The hash value for the key value is calculated using method `Kernel#hash`. Since the hash value of the key should not be changed, instances of the classes like `Array`, `Hash`, etc. are not suited for the key. When the `string` used as the key, hash table copies and freeze it and use copied string as the key string. Attempt to Modify the freezed key strings raises an exception.

## SuperClass:

  `Object`

## Included Modules:

  `Enumerable`

## Class Methods:

  `Hash[key, value...]`

    Creates a new hash table, interpreting arguments as key - value pair. The number of arguments must be times of two.

  `new([ifnone])`

    Creates a new, empty hash table. ifnone is the default value for the non-registered key.

## Methods:

  `self [key]`

    Returns the value associated to the key. Returns the default value (or `nil`), if `key` is not registered in the hash table.

  `self [key]= value`

    Adds binding of key to value. If the value is `nil`, the binding from key will be removed. That means the hash table cannot holds the association to `nil`.

  `clear`

    Makes the hash table empty.

  `default`

    Returns the default value for the hash table.

  `default=(value)`

    Sets the default value for the hash table.

  `delete(key)`

    Deletes the association from `key`. Returns `nil` if key is not found in the hash table.

    If the block supplied to the method, it will be evaluated when no association matches to key.

  `delete_if {|key, value|...}`
  `reject!{|key, value|...}`

    Deletes association from hash table, if the evaluated value of the block with the array of `[key,value]` as an argument is true.

  `dup`

    Returns a newly created hash table which has the save keys and values to the receiver. `clone` returns the complete copy of the original hash table, including freeze status and instance variables. On the other hand, `dup` copies the hash table containts.

  `each {|key, value|...}`
  `each_pair {|key, value|...}`

    Iterates over each pair of keys and values (`[key,value]`) of the hash.

  `each_key {|key|...}`

    Iterates over each key in the hash table.

  `each_value {|value|...}`

    Iterates over each value in the hash table.

  `empty?`

    Returns true, if hash table is empty.

  `fetch(key[,default])`

    Returns the value associated to the key. Returns default, if key is not registered in the hash table. If a block is given, evaluates and returns its value on value absence.

  `freeze`

    Prohibits modification of the hash table. Modification to the freezed string raises an exception.

  `frozen`

    Returns true if the hash table is frozen.

  `has_key?(key)`
  `key?(key)`
  `include?(key)`

    Returns true if hash table has key in the associations.

  `has_value?(value)`
  `value?(value)`

    Returns true if hash table has value in the associations.

  `index(val)`

    Returns the key corresponding to val. Returns `nil` if there is no matching value.

  `indexes(key_1,..., key_n)`
  `indices(key_1,..., key_n)`

    Returns an array of values which keys are specified by arguments.

  `keys`

    Returns an array of keys in the hash table.

  `length`
  `size`

    Returns the number of associations in the hash table.

  `invert`

    Returns the value-to-key mapping of the hash.

  `replace(other)`

    Copis the content of other into the hash.

  `shift`

    Removes and returns an association from the hash table. The association is in the form `[key,value]`.

  `store(key,value)`

    Adds binding of key to value.

  `to_a`

    Returns an array of two element arrays of associations which is `[key,value]`.

  `update(other)`

    Merges the contents of another hash, overriding existing keys.

  `values`

    Returns an array of the values in the hash table.

prev - up - next - index

matz@netlab.co.jp