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.
Object
Enumerable
Hash[key, value...]
Creates a new hash table, interpreting arguments as key - value pair. The number of arguments must be times of two.
new([size])
Creates a new, empty hash table. If size is specified internal space for about size items will be allocated first.
self [key]
Returns the value associated to the key. Returns
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.
delete(key)
Deletes the association from key
. Returns
nil
if key is not found in the hash table.
If the block supplied, it will be evaluated when no association matches to key.
delete_if {|key, value|...}
Deletes association from hash table, if the evaluated value of the
iterator block with the array of [key,value]
as an
argument.
each {|key, value|...}
each_pair {|key, value|...}
The iterator which gives each pair of keys and values
([key,value]
) to the iterator block.
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.
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.
indexes(ary)
indexes(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 associative array.
shift
Removes and returns an association from the hash table. The
association is in the form [key,value]
.
to_a
Returns an array of two element arrays of associations which is
[key,value]
.
values
Returns an array of the values in the hash table.