A Hash is a collection of key-value pairs like this: "employee" = > "salary". It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index.
The order in which you traverse a hash by either key or value may seem arbitrary and will generally not be in the insertion order. If you attempt to access a hash with a key that does not exist, the method will return nil.
Creating Hashes
As with arrays, there is a variety of ways to create hashes. You can create an empty hash with the new class method −
months = Hash.new
You can also use new to create a hash with a default value, which is otherwise just nil −
months = Hash.new( "month" )
or
months = Hash.new "month"
Example
When you access any key in a hash that has a default value, if the key or value doesn't exist, accessing the hash will return the default value −
Tests whether two hashes are equal, based on whether they have the same number of key-value pairs, and whether the key-value pairs match the corresponding pair in each hash.
2
hash.[key]
Using a key, references a value from hash. If the key is not found, returns a default value.
3
hash.[key] = value
Associates the value given by value with the key given by key.
4
hash.clear
Removes all key-value pairs from hash.
5
hash.default(key = nil)
Returns the default value for hash, nil if not set by default=. ([] returns a default value if the key does not exist in hash.)
6
hash.default = obj
Sets a default value for hash.
7
hash.default_proc
Returns a block if hash was created by a block.
8
hash.delete(key) [or]
array.delete(key) { |key| block }
Deletes a key-value pair from hash by key. If block is used, returns the result of a block if pair is not found. Compare delete_if.
9
hash.delete_if { |key,value| block }
Deletes a key-value pair from hash for every pair the block evaluates to true.
10
hash.each { |key,value| block }
Iterates over hash, calling the block once for each key, passing the key-value as a two-element array.
11
hash.each_key { |key| block }
Iterates over hash, calling the block once for each key, passing key as a parameter.
12
hash.each_key { |key_value_array| block }
Iterates over hash, calling the block once for each key, passing the key and value as parameters.
13
hash.each_key { |value| block }
Iterates over hash, calling the block once for each key, passing value as a parameter.
14
hash.empty?
Tests whether hash is empty (contains no key-value pairs), returning true or false.
15
hash.fetch(key [, default] ) [or]
hash.fetch(key) { | key | block }
Returns a value from hash for the given key. If the key can't be found, and there are no other arguments, it raises an IndexError exception; if default is given, it is returned; if the optional block is specified, its result is returned.
16
hash.has_key?(key) [or] hash.include?(key) [or]
hash.key?(key) [or] hash.member?(key)
Tests whether a given key is present in hash, returning true or false.
17
hash.has_value?(value)
Tests whether hash contains the given value.
18
hash.index(value)
Returns the key for the given value in hash, nil if no matching value is found.
19
hash.indexes(keys)
Returns a new array consisting of values for the given key(s). Will insert the default value for keys that are not found. This method is deprecated. Use select.
20
hash.indices(keys)
Returns a new array consisting of values for the given key(s). Will insert the default value for keys that are not found. This method is deprecated. Use select.
21
hash.inspect
Returns a pretty print string version of hash.
22
hash.invert
Creates a new hash, inverting keys and values from hash; that is, in the new hash, the keys from hash become values and values become keys.