Introduction to Hash in Ruby and Rails

Tram Ho

Hash is a data structure stored by related keys. This is in contrast to arrays that store items in an ordered index. The entries in a hash are often referred to as key-value pairs. This creates a linked representation of the data. Most commonly, a hash is generated using symbols as keys and any data type as value. All key-value pairs in a hash are surrounded by curly braces {} and separated by commas. Hash can be created using two syntaxes. The older syntax comes with an => sign to separate keys and values.

The newer syntax was introduced in Ruby 1.9 and is much simpler. As you can see, the results are the same.

You can also have hashes with multiple key-value pairs.

Let’s say you want to add an existing hash.

And what if you want to remove something from an existing hash?

Now how do you get some information from a hash?

What if you want to merge two hashes together?

irb: 011> person.merge! (new_hash) => {: height => ‘6 ft’,: weight => ‘160 lbs’,: hair => ‘brown’,: name => ‘bob’} Note that we used the bang (!) suffix to make this change destructive. We could choose to use the merge method instead, which will return a new merged hash, but leave the original person’s hash unchanged.

Repeats the elements in the Hash

Because hashes can have many elements in them, there will come a time when you want to iterate over a hash to do something with each. Repeating hashes is similar to repeating arrays with some slight differences.

We use each method as before, but this time we assign a variable to both the key and the value. In this example, we are setting the key for the key variable and the value to the value variable. Run this program at the command line with ruby ​​iterating_over_hashes.rb to see the result. The output is:

Hash as an optional parameter

You can also use hashes to accept optional parameters when creating methods. This can be useful when you want to give your methods more flexibility and understandability. More options, if you want! Let’s create a method to do that.

Did we use Ruby hash’s empty? to detect if the optional parameter, which is a hash, has anything passed to it. You have not seen this method yet but you can figure out how it works. You can also see the Ruby documentation for the method lookup. Finally, we called the method twice. One use has no optional parameters and the second uses the hash to pass optional parameters. You can see how using this feature can make your methods much more flexible.

And finally, you can also pass the arguments to the greeting method like this: greeting (“Bob”, age: 62, city: “New York City”)

Note that the curly braces, {}, are optional when the hash is the last argument.

Common hashing methods

Let’s look at some of the popular methods that come with the Ruby’s Hash class.

has_key?

Has_key? allows you to check if a hash contains a particular key. It returns a boolean value.

select

The select method allows you to pass a block and will return any key-value pairs that evaluate to true when running through the block.

fetch

The method allows you to pass a certain key and it will return the value for that key if it exists. You can also specify a return option if that key is not present. Take a look at the Ruby documentation here to see what else could happen.

car

The to_a method returns an array version of your hash when called.

keys and values

If you want to retrieve all keys or all values ​​from a hash

Note that the returned values ​​are in an array format. Because it returns an array, you can print out all the keys in a hash function: name_and_age.keys.each {| k | set k}

Share the news now

Source : Viblo