View my Resume

Hashes

Hashes, otherwise known as key value pairs, are the most complex data type written in the ruby language but don’t worry, they are not hard to master. Hashes are similar to an array however, the difference is that an array is a list of single elements, while a hash is a list of a pair of elements. Let's look at a quick example of a Hash

apple_store = {
"iphone" => 800,
"ipad" => 900,
"macbook" => 1200
}

We have defined a hash apple_store represented by curly brackets. Let's take a look at the first item, iphone. You can see that it's paired with another value, 800. The iphone is the key and the 800 associated with it is known as a value. This is why hashes are commonly called lists of key-value pairs. In ruby, each key-value pair is separated by =>. This is known as a hash rocket in Ruby.

Now let's play around with hashes to visually understand why they are so powerful. Using our example above, to call on a specific value in a hash you do so by calling apple_store["iphone"], and Ruby would return 800.

However, you cannot locate a key by referencing its value, why is this? Since it is possible to have multiple values that can be the same, you would not be able to call on the proper key that you would be looking for since there can be many keys that contain the value of 800. Because of this, it's unsurprising that hashes are one-directional noted by the hash rockets that go in one direction and not both ways(who knows if this is true, but wow is it accurate!).

So let's say you have a defined hash already but you have more items to sell at your apple_store. You can add to a hash as well by calling apple_store["apple_watch"] = 400 which updates the hash to be

apple_store = {
"iphone" => 800,
"ipad" => 900,
"macbook" => 1200,
"apple_watch" => 400
}

What about traversal? We can loop through all the key-values using an each loop, and since we are key-value pairs (in ruby) we need to use two block variables, for example

apple_store = {
"iphone" => 800,
"ipad" => 900,
"macbook" => 1200,
"apple_watch" => 400
}

apple_store.each do |product, price|
puts product
end

# output

# "iphone"
# "ipad"
# "macbook"
# "apple_watch"

You'll see that we get all of the products listed in our hash each on a separate line, or we can use a mixture of concatenation and user input to have our simple apple_store app return prices to our users when given these choices. Another important use of hashes is building a CRM database of people with different “attributes”. Or maybe you are building software that handles employees with different profiles, incomes, etc. Hashes allow us to store lots of data cleanly and concisely. Hashes are a powerful fundamental to understand when writing ruby code and are wildly useful when manipulating all sorts of data.

Hopefully, this article has helped you understand the concept of hashes. Learning to code isn’t easy but if you attack it one small concept at a time the benefits will far outweigh the difficulty.

Posted: October 7, 2019