Ruby’s Data Structures: Hashes

A hash — similar to an array — is a Data Structure.

Hashes are built with curly braces, and inside these curly braces, they hold key/value pairs. Similar to arrays, where we would reference the listed data by a numerical index — with hashes we use the key/value pairs to navigate and reach the stored data.

To build hashes there are two unique syntaxes. There is the fat-arrow “=>”, used to assign a key to the value, but there’s also the newer ruby syntax which introduces a colon on the end of the key to separate the key and value. Inside a hash, we can also have multiple key/value pairs, and we separate them with a comma, e.g.

Accessing the information stored in a hash is very similar to accessing elements inside of an array. We use square brackets — but — with hashes we declare the key inside of the square brackets (instead of the element’s index), which is preceded by a colon to reach its paired value. # see example given below…

Inside my terminal, using IRB (Interactive Ruby Shell)

To add a key/value pair to an already existing hash, we type the hash’s name followed by the new key wrapped inside of square brackets & preceded by a colon. Taking that, we introduce the assignment operator (=) followed by the value you wish to assign it.

e.g. hash1[:key1] = ‘value1’

Using the same syntax, we can also change an already existing key’s value and assign a new value…

e.g. hash1[:key1] = ‘value1000’

/ / Deletion. (This word looks wrong). \ \

Deleting a key/value pair from a hash. Inside of IRB

We take the hash, which holds the key we want to remove and connect the delete method with dot notation. Then, inside of parentheses, we reference the key we wish to remove from the hash. See example above ^^^

###HASHES INSIDE OF HASHES :O###

A football team called Flatiron United have a team of 15 players (4 benched and 11 on the pitch) and each player has it’s own unique attributes. Let’s keep this short and say those attributes are; name, number and position.

Now, flatiron_united is the name of the hash. Inside this hash we have 15 keys — each representing a player. Each player will also be a hash, holding the aforementioned attributes (name, number and position (these attributes will be keys, and they will hold the values)).

Flatiron United. Woo!! Go Flatiron ❤

Accessing the name key inside of player1, so we can retrieve the value “Nico”, is not as intimidating a task as it may seem. We know to reach our desired destination, we must start at the beginning. Our hash name is flatiron_united, let’s start there. Nico is stored inside of the key ‘player1’, and this key is also a hash. Inside, it holds its own key/value pairs. We’re after the name key. So we apply the name key to our call. It should look like this…

e.g. flatiron_united[:player1][:name] # which will return “Nico”!

Of course, there is a lot more to hashes than what I’ve shown so far. There’s iteration, iterating over key and value. There’s also hash specific methods, much like arrays. I’m going to leave you with a note on my favourite hash method…

.fetch

Why? Well, for me, hashes are a challenge because they can be difficult to navigate…

Navigating a hash, and looking for something in particular can be a whole lot easier with fetch, especially if you’re unsure with the value you’re looking for.

My advice with hashes, is to make note of where you are inside of the hash. Transitioning from level to level can get confusing, even more so if you’ve not been making notes. Good luck!

--

--

--

Fullstack Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CTF: Pickle Rick

Basics of HTML

Eleven practical tricks to write Clean Code — Part 2

Auto-configuration in SpringBoot

vscode vs jupyter for OpenCV 🚀

Industry Use Case on Automation using Ansible:

How to test API requests in Mailchimp

The limit of complexity in-silico

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daniel Glover

Daniel Glover

Fullstack Developer

More from Medium

Enum on Rails — A shallow dive.💎

Running Rake Tasks with Cron and File Locks

Mixins in Common Lisp

Why Do I Use PostgreSQL For My Rails Backend & How To Set It Up