Hash and Arrays, what's the difference?
Nichole Shelton
November 16, 2014
What's an Array?
An array is an ordered integer indexed collection. Say what? It's a list that is ordered numerically. Arrays can be used to build data structures. When you're writing code and you want to look up the values you look them up based on the number that corresponds to the list. Sounds confusing I know, but don't worry. Let's take a look at an example. I'm going to make a list of my favorite tv shows.
fav_tv_show = ["The Mindy Project", "Chopped", "How to Get Away with Murder"]
To look up the items in your array, you would need to call them using the index number that is associated with the object. The first object is 0, the second is 1 and so on. Let's take a look at how you would look up a value with an array.
fav_tv_show[2] How to Get Away with Murder
What's a Hash?
Nope, totally not referring to my favorite breakfast item sweet potato hash. We're talking a hash (sometimes referred to as a hash table) that's pretty similar to an array. The difference with an array is that it is a collection of key and value pairs. The keys you assign and are arbitrary. Each pair must be unique and can be a name or a number. Let's see how a hash is organized using our tv show example.
fav_tv_show {"drama" => "How to Get Away With Murder", "comedy" => "The Mindy Project", "reality" => Chopped}
When looking up a hash you use the key to return the value that is paired to it. Let's take a look at how you would look up a value with a hash.fav_tv_show["reality"] Chopped
And that's a basic description of the differences between a hash and an array.

