Keeping Track of It All

Arrays, Hashes, and When to Use Them

Posted by 'Tine Zekis on April 25, 2015 · 6 mins read

Assignment: Technical Blog | Arrays & Hashes

In all programming languages, we can assign values to variables. For instance, if I want to create a sting that I will use multiple times and/or manipulate in some way, I will want to name it. In Ruby, we assign variables by listing the name followed by an equal sign and the assigned value. For example, greeting = 'Hello, loyal blog readers!' would tell the program that the variable greeting points to the string 'Hello, loyal blog readers!' So any time I wanted to use the string or call some method on it, I can use greeting as a sort of nickname. We can also assign other types of values like integers or floats (numbers with a decimal point). For instance, example_int = -47 assigns the integer value -47 to the variable example_int. Now, if I call the absolute value method thusly, example_int.abs, the return will be the absolute value of the variable (47).

So we have a way to store values into our variables. But what if we have a lot of variables we need to keep track of. For instance, what if I wanted to store a list of responses to a single question by many respondents, or an entire grocery list? I might not want to create and assign variables for each of these items, as this could become quite cumbersome (response1 = 3, response2 = 7, ..., response 36 = 9). This is where we might choose to use something called an array. Arrays store a list of variables into a series of slots, or indices. So I could create just one array for my entire list of responses: responses = [3, 7,..., 9]. Then, I can reference each item within the list based on its index (slot) in the array. But here's where things get tricky: we actually begin counting from index 0. Thus, responses[0] refers to the first index and returns a value of 3. In order to get our second and 36th values, we would need to call responses[1] and responses[35], respectively. If our list has 36 items, then the 37th index (responses[36]) would return the nil value, or "nothing". There can even be indices within a list that return the nil value. In order to understand this better, let's talk about another way we can assign values to an array.

Rather than listing all values at once, we can also assign values to an array individually by index, or slot position. For instance, I can create an empty array like this: chicago_teams[] or chicago_teams = Array.new (please note that "Array" gets capitalized in this second option). Then, I can assign individual values thusly:
chicago_teams[0] = 'Bulls'
chicago_teams[1] = 'White Sox'
chicago_teams[3] = 'Blackhawks'
puts chicago_teams
Printing out the array would produce the following:
Bulls
White Sox
nil
Blackhawks
The third index (chicago_teams[2]) has not been assigned and thus contains the nil value.

Hopefully by now, we're getting used to referring to items in an array by their index numbers. But what if we don't want to reference our items this way? For example, what if we had a list of all basketball teams in the NBA? Would we really want to remember which index number went with each team? Wouldn't it be easier to reference those teams by city instead? Well, fortunately, we have a tool for that as well: a hash. A hash organizes information in key-value pairs. So in this case, we might reference the value 'Bulls' with the key chicago and 'Bucks' with the key milwaukee. We could use any of these formats to set up the hash:
nba_teams = {'chicago', 'Bulls', 'milwauke', 'Bucks'}
nba_teams = { ['chicago', 'Bulls'], ['milwaukee', 'Bucks'] }
nba_teams = {'chicago' => 'Bulls', 'milwaukee' => 'Bucks'}
Or, just like with the array, we can create an empty hash and then assign key-value pairs later:
nba_teams{} or nba_teams = Hash.new (notice that "Hash" also gets capitalized in this situation)
nba_teams['chicago'] = 'Bulls'
nba_teams['milwaukee'] = 'Bucks'
No matter how we created and assigned the hash, we can now reference the values by their keys. So nba_teams['chicago'] now points to the value of string 'Bulls'.

So we can see that hashes and arrays have different properties that make them useful for different situations. Arrays help us to keep track of a list of values that have a specific order. We can use array commands to rearrange this order, sort the items in the list, reference items by index number, and many other tasks. All the while, the array keeps track of where each item is stored. Hashes do not maintain information in a particular order. Instead, values are stored and referenced by their keys. Perhaps we want to keep track of Sally and Joe's respective responses to a question, instead of respondents 1 and 2. In this case, a hash might be a more useful way to store this information. In order to determine whether you need an array or a hash, it will be best to consider what you need to do with this information, and how it can most conveniently be referenced when needed.

I hope this brief introduction has given you an idea of how these two objects differ and when you might need either one. Stay tuned for more of my coding discoveries as the journey continues!