data all day

Ruby Time: Arrays and Hashes
Friday, April 3rd, 2015

back to blog index

Hello. Are you new to programming? You don’t have to answer that, let’s pretend you are. And let’s say you want to write a program that tells a story, about what Bruce Sprintsteen did today. You could type out a story like this:

Bruce Springsteen woke up. Bruce Springsteen ate breakfast. Bruce Springsteen wrote a song. Bruce Springsteen played it for a friend. Bruce Springsteen ate a late lunch.

If you’re like me, you’d probably get bored of typing “Bruce Springsteen." What could you do? In programming you could save that name to a variable, like this:

bruce = “Bruce Sprintsteen”

Then you could just use the variable “bruce” in your program, instead of typing “Bruce Springsteen” every time. Storing things is easy!

Let’s tackle a different problem. Say I have five people in my family, and I want to use those in my program. I could store them as variables too.

homer = “Homer Simpson”
marge = “Marge Simpson”
bart = “Bart Simpson”
lisa = “Lisa Simpson”
maggie = “Maggie Simpson”

Ok, that’s pretty good… but what if I want to name everyone in my family? I’d still have to name all the variables individually, or look back through my program, copy and paste… that’s kind of a lot of work. What if I could store that as a list?!

family = [“Homer”, “Marge”, “Bart”, “Lisa”, “Maggie”]

This list is called an array. An array is an ordered collection of values. This works better. In an array, values are stored by numeric indexes. So if someone wanted to know who my third family member is, I could look that up by saying:

family[2]

And it would tell me “Bart.” (Arrays are sort of weird, their number system starts from 0. There is a reason, but there’s no need to get into it here.) Pretty nice. But, who asks that, my “third” family member? What a weird way to identify them. It doesn’t mean anything to me, or probably anyone, for that matter. Array’s are a great way to store a list, but the index doesn’t really help me identify them. Like with variables, I would have to remember the identifier of what I'm looking for, or see the list to pull things out specifically.

What if I wanted to store this information similarly, but I wanted to make the indexes a little easier to use or remember? For example, let’s the person from the last example wants to know my third-BEST something, let’s say, third-best friend? I could use a different method:

friends = {
:best => “John”,
:second_best => “Paul”,
:third_best => “George”,
:not_really_a_friend => “Ringo”
}

Then, when someone wanted to know my third-best friend, I could look it up with:

friends[:third_best]

And it would return “George.” Great! This data type is called a hash, at least in Ruby. It is an un-ordered collection, again with values, but they are indexed with unique identifiers, called “keys.” Thus, hashes are a collection of key/value pairs. They work kind of like a dictionary (and hashes are actually referred to as "dictionaries" in some programming languages). The user can look up information based on an identifier that they know. For our purposes here, the hash works great.

That isn’t to say that the hash is the best solution for storing data. The keys aren’t always needed, sometimes indexes work just fine. And there are other data types that work great for other purposes. But arrays and hashes are very common, so it’s important to know the difference. Hopefully - as you were new to programming five minutes ago - now you do.

back to blog index