Hashes are collections of objects that are primarily organized by sets of key/value pairs. The keys must be unique and they can be any type of object. Most often, strings or symbols are used as keys. Like an Array, Hashes feature all of the collection-related functionality provided by the Enumerable module
In Ruby, hashes are considered ordered because they maintain information about the order in which objects were added. Hashes are said to have a meta-index; each element in a hash features a key, a value, both of which can be any type of object, and an meta-index, which is always an integer.
There are three ways to create hash tables in Ruby. The literal approach is the most common. It uses curly brackets that contains key/value pairs linked by the => operator, and separated from one another by commas. Below is an example where the keys are symbols and the values are strings (hashes support any types of objects for both keys and values).
hash = {:key_1 => “value_1”, :key_2 => “value_2”, :key_3 => “value_3”}
The second approach is the Hash.new method. This approach creates an empty hash. The one benefit of using this approach is that this method accepts an argument that defines the default value for a hash. The default value is returned where there are attempts to retrieve a value using a key that does not exist. It is also assigned as a value if a key is added without specifying a value pair.
This method also allows you to supply a code block that can be used to initialize hash keys that are supplied without a value, even when you don’t specify as default value.
Hash[] is the last method that can be used to create a hash. This method accepts an even numbered set of comma separated objects as arguments. These objects are joined into key/value pairs to generate the Hash table. If an uneven number of arguments are provided then an error is returned.
hash = Hash[key_1, value_1, key_2, value_2, key_3, value_3]To add a new key/value pair to a Hash you use the [] = method, as shown below. It is important to remember that keys must be unique, so if you try to add a new key/value pair with the same key it will override the previous value associated to that key.
hash[key_1] = value_1The most common approach to retrieve values from a hash is the [] method. This method returns the default value if the key you provide does not exist (if no default value has been set then it returns nil). The fetch(key) method can also be used to retrieve values, however, it will raise an exception if the key is not matched. You can also request multiple values at the same time using the values_at(key_1, key_2, .. key_n) method. This method returns an array with the retrieved values.
There are two different methods for joining hash tables. The method merge(hash) returns a hash that includes all elements from the two original hashes. As the name implies, the method update(hash) actually updates the hash that receives the method call (hash_1 in the example below). It is important to note that if the same key exists on both hashes then the value from hash which is provided as an argument wins out (hash_2 in the example below).
hash_1 = {:fruit => "banana", :vegetable => "tomato", :starch => "rice"}hash_2 = {:meat => "chicken breast", :candy => "chocolate", :fruit => "strawberry"}hash_3 = hash_1.merge(hash_2)hash_1.update(hash_2)Hashes also support the replace method, which functions similar to the way it works on arrays. One method to watch out for when working with hashes is called invert. This method switches each key for the value, and vice versa. If you have more than one key with the same value then one of these will be lost in the conversion.
There are several different queries that you can perform on hash tables. The most common ones are the size method, which returns the number of key/value pairs; the has_key?(key) that returns true if the key exists (the include?, key?, and member? methods do the same thing); and the has_value?(value) method, which returns true if the value exists in the hash.