Let's talk about hashes
...and array's, too, I guess
01.12.2014
Not hashbrowns, not #hashtags, not hashish. I mean hashes in Ruby. Hashes are about storing data. Hashes are similair to a table of contents, like the one below.

Except, instead of a subject tied to page number, hashes pair a key with a value. You can later manipulate this data by applying methods to the hash. For example, we can use a hash to keep track of how well your favorite basketball players are doing year in terms of points. We are going to create a hash (points2015) that stores the average points per game for basketball players.

Each player is a key. The Average Points/Games are the value. Now we can use Ruby to extract the information we want from this data. Let's say I want to know who the top scorer is. I would create a function (best_player) that uses the points values to return the name of the top scorer.

In this method, we are asking the method (best_player) to use the array (points2015). We are asking the method to put (aka: display) a key value (name of a player) according to the conditions in the parenthesis. Inside the paranthesis, we are taking the values for the array, sorting them in ascending order, then reversing that order, so the highest number will be on top.

Finally, we are asking to take the first value in the newly ordered list, and display the key.

That is correct! If you go back and look at the original, Kobe has the most points, with 26.6 points/game average. I know this example is very easy, and it would be much easier to look at the list of players to find out who has the most. But this method is useful because you can store a much larger amount of data and call much more methods onto the data to manipulate it depending on what you want to know. Want to find out how many people with the last name Hernandez, in the 30's live in you city? You can do that with hashes. Want to know how many products under $25 dollars the corner store sells? Hash it!
On the other hand, if you simply want to store a set of data points, with nothing attached to it, you can use an array. An array lets you store a bunch of strings and/or integers together as a single unit.
If you want to store a list of the different produce your mom's store sells, use an array. If you want to store a list of different produce your mom's store sells as well as the price for each product, use a hash.
Let's say you're an educator and you want to see how well your class is doing on test scores, but you are not worried about seeing which students in particular are doing well. You can store all your students' scores in one array:

Next, you can create a method that add up all the scores to give you the sum and then divides that sum by the number of elements in your array, giving you the average score in your class.

Next, you can call that method and use the stored array of student scores as the array parameter. This does all the math you need to your scores.

And your class's average is:

80. B-. Eh. They can do better. You should give them a little more homework or give them detention. Again, like in my hash example, it makes more sense to do these manually than to take time to code all these. But imagine when you have to have to do these processes for every college basketball player in the US, or the grades of all 3rd graders in the Los Angeles School District. You are not going to want to do it by hand, and Ruby is a simple way to store data (either in a hash or an array) and manipulate it multiple times to find what you need.