Sunday, November 16, 2014
Week 3 covered Ruby basics and two things we picked up were arrays and hashes. You can think of them as lists. Since Christmas is coming, let's think about people's wishlists, in particular mine.
An array is an ordered list of objects. Objects can be strings (names, book titles, etc), integers (prices, quantity, etc), and arrays (more lists), among other things. Let's say I want to make a Wish List for my birthday, which also happens to be on Christmas (yeah... I always get only one present, not two). Naturally, I would put the things I want the most on top.
Now, putting on our Ruby hat, let's make this into an array called wishList and enter the items that I hope others will buy for me.
You can create an array with Array.new and/or type out all the elements in the array:
wishList = Array.new
wishList = ["Baby the Dog", "Net subscription", "Toms shoes"]
In Ruby, instead of starting the list with 1, 2, 3, arrays actually start at index 0 and then 1, 2, 3. So technically index 1 is the second thing on the list (Net subscription). So, lets say if you really want to see me happy and want to buy me the thing I want very most, you can search by looking up what index 0 would give me. See below in code speak:
wishList[0] => "Baby the Dog"
wishList[1] => "Net subscription"
wishList[2] => "Tom brogue shoes"
Okay, so I changed my mind about Baby being my #1 most desired gift. I am a student after all...and Baby is 10 years old so I don't think it's a good idea to take on DBC and a senior dog. Let's take off her of the list with .delete method.
wishList.delete("Baby the Dog")
wishList => ["Net subscription", "Toms brogue shoes"]
My list is so small now. What if more than two people want to buy me things? If I can't have Baby, then a dog pillow will suffice.
There are several ways to add an element: .push, <<, .unshift, .insert. Let's use .push as an example:
wishList.push("Pug Pillow") =>
["Net subscription", "Toms brogue shoes", "Pug Pillow"]
With .push, it puts the Pug Pillow at the very end. What if I want the Pug Pillow more than the Toms brogue shoes? In this case, instead of using .push, you can use .insert(index#, 'item'):
wishList.insert(1, "Pug Pillow") =>
["Net subscription", "Pug Pillow", "Toms brogue shoes"]
Okay, now you know the fundamentals of creating an array!
Hashes are similar to arrays in the sense that they are also a list. Whereas you can grab things from the array by the index number, with hashes you have keys and their associated values. For example, I can create my wishList of what I want and where you can purchase them. How convenient! This should make it easier for my gift-buyers, present-givers to shower me with well-deserved, long-awaited presents.
Arrays have [] brackets, hashes have {} curly braces. Similar to creating an array, you can either start with wishList = Hash.new and then add the elements in wishList or have the name equal the elements in the array, like below:
wishList= {"Net subscription"=>"CreativeBloq.com", "Toms brogue shoes"=>"Toms.com", "Pug Pillow"=>"ModCloth.com"}
So, you see here that my Net subscription is the key and CreativeBloq.com is my value, and so on.
Let's say you want to get me the Pug Pillow, but you don't know want to look at the long, extensive wishList, you just want to know where to buy it. You can search for the value for the key Pug Pillow with:
wishList["Pug Pillow"] => "ModCloth.com"
You can also search for the key by its value. So, if you're online at Toms.com and you can't remember which kind of shoes I wanted, you can find it in Ruby with .key:
wishList.key("Toms.com") => "Toms brogue shoes"
I changed my mind again. I think I do want Baby the Dog, not that lame wannabe-dog pillow. Baby is relatively low maintenance, she sleeps a lot anyways. To delete the Pug Pillow, you can use .delete:
wishList.delete("Pug Pillow")
wishList => {"Net subscription"=>"CreativeBloq.com", "Toms brogue shoes" => "Toms.com"}
Now it's time to add Baby the Dog back with .store:
wishList.store("Baby the Cutest, Best Pekingese Dog Ever", "NorCal Family Dog Rescue")
wishList => {"Net subscription"=>"CreativeBloq.com", "Toms brogue shoes" => "Toms.com", "Baby the Cutest, Best Pekingese Dog Ever" =>"NorCal Family Dog Rescue"}
That's it! That's array and hash in Ruby for you. Now go make your Wish List and send it to a programmer friend to figure it out, because your non-programming friends sure as hell won't. And go adopt Baby the Dog! She's old, a little smelly, has eye boogers, but her little tongue is always protruding out!