Arrays and Hashes in Ruby

DBC Week 3: Challenge #7

May 14, 2015

    So an array is an ordered list of objects. What exactly does that mean?

    Think of an array as a container for storing data, like a filing cabinet where the data can be placed sequentially one after the other. One of the big concepts to remember is that in the filing cabinet where you plan on storing this data, all of the file folders are already labeled with numbers, and you can't change that. File folders are labeled starting with zero and count up to the last file in the drawer (this is the index of the drawer). The folders also have a label that starts at the last file with -1 and moves up to the front, each number getting smaller (this is the negative index of the file). There are array methods (kind of like commands to give to the array) that give you the ability to add data into the array, take data out, or manipulate it in various ways. Keep in mind that this data needs to be in the form of an object; a number, a string or even another array (even a hash). You can create an array in a number of different ways, but the most straightforward is with literal notation. Here's what that might look like:

    array_name = ["data_1", "data_2", "data_3", ....]

    You give your array a name, open brackets, start putting in data bound with quotes, and close the bracket. You can add as many pieces of data that you wish. The important thing to remember is that when you want to retrieve your data, you can only do it by the index number. Remember that the index numbers start at 0, so the first file in the drawer is file 0. The negative index starts at -1, so counting from the back, the first file is file -1.

    Imaging this:
You're a coach of a little league team and you have a really hard time remembering things. You have just heard the schedule of games for your season and you want to make a list of all the games to be played that season from the first to the last. Your list can be easily made into an array like this:

    game_schedule = ["Bears", "Colts", "Lions", "Vikings", "Broncos"]

    Now if you want to access the information in the array, say the third team you are playing for example, you look for it by it's position number: the third team has an index position of 2. Check it out:

    You want to find who you are playing for the third game (don't forget the keyword "puts" or nothing will be printed to the screen):

    puts game_schedule[2] => "Lions"

    Say you want to find the second to last game you are going to play. Just use the negative index this time. Remember it starts from -1 and counts backwards, and don't forget your "puts":

    puts game_schedule[-2] => "Vikings"

    You realize that you made a mistake and your first game is not against the Bears, but actually against the Packers. There's no reason to start from scratch thanks to the Array#unshift method. It works like this:

    game_schedule.unshift("Seahawks")

    You can now take a look at what is in your array:

    puts game_schedule =>
    Seahawks
    Bears
    Colts
    Lions
    Viking
    Broncos

    Need to add a game to the end of the list? There's a method for that too:"

    game_schedule.push("Falcons")

    If you want to look at what your array looks like now, just "puts game_schedule" again just like the above, and you will see the updated list. The only thing to remember is that since you have now changed the list, you have now changed the index numbers of all the games in the schedule. "Seahawks" is now at game_schedule[0], and the "Bears" have been bumped to the position of game_schedule[1].

    This is just the tip of the iceberg. There are methods that will allow you to take the last item off the array list (Array#pop), delete specific items (Array#delete_at(position)), change the item at a specific position (array_name[position_number] = "new_item_name"), and many many more. There are many more ways that you can manipulate this information not listed here, but if I don't stop here, I'll have no time for Hashes. If you want to read more on Arrays, different ways to build them and what you can do with them, go and check this out: Ruby Doumentation for Arrays. Have fun!

    And now on to hashes.

    Hashes are similar to arrays in the fact that they are also containers for storing data. Hashes are going to work like a filing cabinet as well, but this time your folders are not labeled with numbers, they aren't labeled yet. What the hell does that mean you ask? Well hashes are containers that hold data in what is called a key-value pair, and they are both objects. Think of it this way:

    Arrays also have a kind of key-value pair: the numbers are the keys, the position_number of that data element to be exact, and the values are the data that is listed at that position number. An array has this characteristicset up already, you can not change it. Hashes however let you decide what the key is.

    Think of it again as a filing cabinet. But this time the "hash" drawer has blank labels on the file folders, and you can label them with whatever your heart desires. Just don't choose a number for the label, that's just going to confuse your hash drawer (if you want to use numbers go and create an array). What this allows you to do is find the value you are looking for without knowing its position number, because a hash doesn't care about order. The keys are really what keep the data (their value pair) indexed, the order of the array is just the order that they were entered in. C'mon Coach, let's take another look at this:

    You are taking a look at your array list and you think to yourself, "Well this helps, but with so many games and so many locations, how am I going to keep it all straight." Enter the hash. You can create a hash, in almost the same literal notation as we used for the array, that is composed of data in key-value pairs. Take the name of the team you are playing and associate it to the address of the field where the game will be played. Check it out:

    game_location = { "Seahawks" => "CenturyLink Field", "Bears" => "Soldier Field", "Colts" => "Lucas Oil Stadium", "Lions" => "Ford Field", "Vikings" => "New Minnesota Stadium"}

    The key is the name of the team and the value is name of the stadium you are going to play in. That's right, all your games are away, and your little league team is super big time, and you are going to play in NFL stadiums. Please stay with me here.

    The key-value pairs are tied together, one object pointing to the other, as opposed to an array where a position number is pointing to an object. So now if you want to check where you are playing the Vikings, you search for the location by means of file folder labeled "Vikings" :

    game_location["Vikings"] => "New Minnesota Field"

    You have the ability to list out the contents of the hash just like typing in "puts and the hash_name" like we did with the array above. You can also list out what all the keys in the hash are, or what all the values in the hash are. Here, check it out:

    puts game_location.keys => ["Seahawks", "Bears", "Colts", "Lions","Vikings"]
    puts game_location.values => ["CenturyLink Field", "Soldier Field", "Lucas Oil Stadium", "Ford Field", "New Minnesota Stadium"]

    You want to add a game? Sure, no problem. Type it out like this:

    game_location["Chargers"] = "Qualcomm Stadium"

    You can go back and see that this new key-value pair, "Chargers" =>"Qualcomm Stadium", has been added to the hash with a simple "puts game_location".

    You can't remember if you put in the key value pair for "Bears"? No problem. You can ask the hash if it includes a key or value with the "has_key?" or the "has_value?" methods, and will give respond true or false if it has it or not:

    game_location.has_key?("Bears) => true
    game_location.has_key?("Packers") => false

    game_location.has_value?("Lucas Oil Stadium") => true
    game_location.has_value?("Chicago Stadium") => false

    Once again this is a very humble start, and there is a whole lot more that you can do with hashes. There are different ways to create them, different methods to call on them (commands to give them), and ways to manipulate the data. If you want to read more on hashes and the power that they can wield, go check this out: Ruby Documentation on Hashes. Enjoy yourself!