Eunice Choi
Web Development Student | Dev BootCamp SF

[Arrays] vs. {Hashes}


September 13, 2015

Which do you think is harder: learning Ruby or teaching a class of kindergarteners? I think they are equally challenging, but in this blog, I'd like you to do both at once in order to understand arrays and hashes.


Arrays and hashes are both indexed collections. Collection means that they can store objects such as strings and integers. Indexed means that they have "keys" to access each of their stored objects. Like a key to a classroom door, you can only access an object in an array or a hash with its indexed key! Now let's unlock the door to your classroom and prepare for your first kindergarten lesson. First thing on the list: get to know your students' names.


[Arrays]


Let's create an array with your class roster. You could call your array "students" and store all of your students' names. It would look something like this:

students = ['James', 'Peter', 'Emily', 'Sam', 'Brian', 'Karen', 'Claire']
                    

Remember how I said that arrays and hashes are indexed? Arrays are unique in that they are integer-indexed, starting with 0. In the array above, an index of 0 would indicate James. An index of 1 would represent Peter, an index of 2 would represent Emily, so on and so forth. To access each element in your array, you need to type the array name next to the index integer in brackets. For instance, you would type the following code to access the first name on the list:

students[0]

Arrays have many useful built in methods. For instance, you could alphabetize the students names by using:

students.sort


or even find the number of students in your class by typing:

students.count
.

You can even have empty arrays!

students = []

{Hashes}


Now that you are familiar with your students' names, why don't we assign your students their cubbies?



Since your cubbies have number labels on them, why don't we just assign each student a number? Let's create a hash called "cubbies_assignment" to do this job:

cubbies_assignment = {
1 => 'James',
2 => 'Peter',
3 => 'Emily',
4 => 'Sam',
5 => 'Brian',
6 => 'Karen',
7 => 'Claire'
}

As you can see, hashes are similar to arrays in that they store objects. The hash above stores your students' names along with their cubby numbers. So what makes hashes so different from arrays? One thing for sure, you can see that hashes use {braces} in stead of [brackets]. They also hold two objects for every entry: a key and a value. Earlier, arrays were integer-indexed starting from 0. Hashes, on the other hand, are indexed by the key you label them with. The left side of the "=>"" represents the key while the right side represents the value:

key => value

In order to index each value you would type:

hash_name[key]

To index Karen, you would input:

cubbies_assignment[6]

Like arrays, hashes also have great build-in methods. To print just the keys of your hash, you can type:

cubbies_assignment.keys
=> [1, 2, 3, 4, 5, 6, 7]

To get the number of cubbies, you could type:

cubbies_assignment.length

What if you want to assign a new student a cubby? You could add to an existing hash by typing:

hash[:8] = 'Diane'


In your classroom, hashes and arrays were used for storing important data: students' names and cubby numbers. In programming, they are used in the same way. Arrays are useful for storing values that can be accessed through "implied" keys from 0 up. Hashes are useful for look up actions. For instance, you are able look up a student's name by typing in his or her cubby number. Both arrays and hashes are powerful tools, and I hope you found this blog helpful for understanding their uses!