Website hosting service by

 Back to Index

prev - up - next - index


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:


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.



Included Modules:


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.


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


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.


Makes the hash table empty.


Returns the default value for the hash table.


Sets the default value for the hash table.


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.


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.


Returns true, if hash table is empty.


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.


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


Returns true if the hash table is frozen.


Returns true if hash table has key in the associations.


Returns true if hash table has value in the associations.


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.


Returns an array of keys in the hash table.


Returns the number of associations in the hash table.


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


Copis the content of other into the hash.


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


Adds binding of key to value.


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


Merges the contents of another hash, overriding existing keys.


Returns an array of the values in the hash table.

prev - up - next - index





2002-2004 Website Hosting Service

Buy domain name at only $5.95/yr with comprehensive domain hosting services. Domain registration and domain search service from $5.95 Cheap domain name registration and domain transfer service

Disclaimer: This documentation is provided only for the benefits of our website hosting customers.
For authoritative source of the documentation, please refer to