Nested Elements

nest

Since I have been writing a lot about hashes I thought why not go into a little bit more detail about hashes. What if a hash contains an array? What if that array has more hashes inside of it? Let’s see how this will look and how to access an element inside of it…

There is a lot going on inside of this hash! Step by step of what this is:

  • The name of the hash is “favorite_foods”
  • The first key is “fruits” and the value is an array. Inside the array there are two more hashes.
  • The next key we see is “candy” and it also has an array as the value. Inside the array are two more hashes.

How would we print out all the different fruits and what colors they are?

nest_hash2

This is going to print out your “fruits” which are: [{“apple” => “red”}, {“grapes” => “purple”}]

You can tell when it is all by itself that it is an array with two hashes inside it.

What if we only wanted to print out “red” for “apple”?

nest_hash3

  • First we have written on the top line what we just saw only now we are having that value set to a variable called “fruits_array”. (This means that since favorite_foods[“fruits”] will return the array of fruits we are now saying put that array and store it into “fruits_array”).
  • Now that we have the array stored in a variable we can apply the EACH method to it. The each method can take a block which is passed through the pipes (these guys ||).
  • Inside the pipes (||) I wrote “fruit_hash”, because we are going to be working with the hashes now for fruits. It is really the same thing that we saw before when we wrote: favorite_foods[“fruits”].
  • Then you will see printed to the screen….red!!

 

Back to Hashes

theater_seats

Hashes are so awesome that I am writing about them again! I first talked about what a hash is and how to access something that is already inside of your hash. What if you want to make your own hash from scratch!? Once you have made your hash you want to add more key, value pairs to your hash. Then you want to show everyone all the awesome information in your hash by printing it out! That’s so much stuff and I am going to break it all down for you…

I want to make my hash about all the different movies I like and for each movie I want to write why I liked it.

  • Make the “movies” hash

  • We created the hash called “movies”.
  • Our key is “Hard Day’s Night”
  • The value is “The Beatles”

A LITTLE REVIEW: What if I want to print out “The Beatles!”?

  • I would write: movies[“Hard Day’s Night”]

Awesome! Now “Hard Day’s Night” isn’t the only movie I like so let’s add another movie to our “movies” hash.

movies_hash2

  • First we have the name of the hash “movies” followed by [] which contain the name of the movie we want to add “High Fidelity”. “High Fidelity” becomes our key and it is equal to “John Cusack and Jack Black” which will become the value.
  • SO if we were to look at our hash again we would now see…

movies_hash3

 

  • Now we want to show everyone our “movies” hash. We want to print to the screen what the name of the movie is and why we like it.
  • IF we were to write: puts movies it would print out our hash, but we want to make it look a little neater for people to read, so let’s write the following…

movies_hash4

 

  • Whoa..there is a lot going on here! Let’s break this all down…
  • We see the name of hash “movies” followed by “.each”.  What “.each” is saying is, “I want to do something to EACH item that is in this hash”.
  • The next word we see is “do”. So for EACH item in the hash we want to “do” something.
  • Then we see “|key, value|”  and this is saying what we want to work with. *Note these words are just place holders and you can really call them anything you would like and I can go over this more in another post!
  • On the next line we see our friend “puts”, this is going to “put” to the screen what is in the parenthesis.
  • What is inside our parenthesis are #{key}: #{value}…what this means is that as we go through our hash we want to print out the key followed by an colon then print the value.
  • That’s all we want to do for now, so we write “end”.
  • Here is what you will see printed to the screen…

movies_hash5

Have fun with hashes!

Hashes in Ruby

After class last night I went back to go over hashes in Ruby. What better place to work than McNally Jackson’s Bookstore! Yesterday was one of the first warm days so I took a nice long walk before settling down to work at this cute little bookstore. They have awesome coffee too which by the end of the day was a great treat!

photo

While I was at this beautiful coffee shop I learned about hashes in Ruby. Here is a little brief intro to hashes! (Click on the image to read about the willy_wonka hash)

wonka_bar