Day 22: Back in the Treehouse

I’m back and wanting to knock out a bunch more work tonight. Let’s get to it!

I’m starting with Treehouse. I did a lesson earlier on arrays. I am continuing onward from there.

Here is the code I did, which looks at adding items to an array:

and the results:

treehouse:~/workspace$ ruby array_addition.rb
[“milk”, “eggs”, “bread”, “carrots”]
treehouse:~/workspace$ ruby array_addition.rb
[“celery”, “milk”, “eggs”, “bread”, “carrots”, “potatoes”]
treehouse:~/workspace$ ruby array_addition.rb
[“celery”, “milk”, “eggs”, “bread”, “carrots”, “potatoes”, “ice cream”, “pie”]

This was as I added in lines and kept re-running it.

And the challenge:

Add the string “carrots” to the end of the grocery_list array.

and my work:

And the challenge:

Challenge Task 1 of 3

Create a variable called “first_item” that contains the first item from the “grocery_list” array (index 0).

Challenge Task 2 of 3

Create a variable called “last_item” that contains the last item from the “grocery_list” array. You can use either the method last or the index.

Challenge Task 3 of 3

Almost there! Set a variable called “list_size” that contains the number of items in the array. You can use the “count” method for this.

Next video: “Removing Items From Arrays”

The IRB commands:

Challenge Task 1 of 2

Set a variable called last_item which uses the pop method to take the last item out of the grocery_list array.

Challenge Task 2 of 2

Set a variable called first_item which uses the shift method to take the first item out of the grocery_list array.

Next section, first video: Ruby Hash Creation

Challenge Task 1 of 2

Create a variable called grocery_item_1 which is a new hash.

Challenge Task 2 of 2

Create a key in the grocery_item_1 hash called “name” and set the value of that key to the string “milk”.

grocery_item_1 = {“name” => “milk”}

Next video: “Working with Hash Keys”

My work in IRB:

and the challenge:

Challenge Task 1 of 1

Using the has_key? method, check if the hash variable has a key called “calories”. If it does, set a new variable called “food” to true.

Next video: “Working with Hash Values”

The work:

And the challenges:

Challenge Task 1 of 2

Using the has_value? method, check to see if the grocery_item hash has a value called “Bread”. If it does, set a new key in the hash called “food” with the value of true.

Challenge Task 2 of 2

Using the values_at method, create an array called grocery_list with the value of the grocery_item hash at the “item” key.

Next video:

The work:

and the results:

=> {“item”=>”Bread”, “quantity”=>1, “brand”=>”Treehouse Bread Company”}
irb(main):003:0> hash.keys
=> [“item”, “quantity”, “brand”]
irb(main):004:0> hash.has_key?(“brand”)
=> true
irb(main):005:0> hash.has_key?(“brands”)
treehouse:~/workspace$ ruby hash_methods.rb
Hash: {“item”=>”Bread”, “quantity”=>1, “brand”=>”Treehouse Bread Company”}
treehouse:~/workspace$ ruby hash_methods.rb
Hash: {“item”=>”Bread”, “quantity”=>1, “brand”=>”Treehouse Bread Company”}
hash.length: 3
hash.invert: {“Bread”=>”item”, 1=>”quantity”, “Treehouse Bread Company”=>”brand”}
treehouse:~/workspace$ ruby hash_methods.rb
Hash: {“item”=>”Bread”, “quantity”=>1, “brand”=>”Treehouse Bread Company”}
hash.length: 3
hash.invert: {“Bread”=>”item”, 1=>”quantity”, “Treehouse Bread Company”=>”brand”}
hash.shift: [“item”, “Bread”]
hash: {“quantity”=>1, “brand”=>”Treehouse Bread Company”}
treehouse:~/workspace$ ruby hash_methods.rb
Hash: {“item”=>”Bread”, “quantity”=>1, “brand”=>”Treehouse Bread Company”}
hash.length: 3
hash.invert: {“Bread”=>”item”, 1=>”quantity”, “Treehouse Bread Company”=>”brand”}
hash.shift: [“item”, “Bread”]
treehouse:~/workspace$

The challenge:

Challenge Task 1 of 1

Using the merge method, create a hash named final_item that contains the grocery_item hash merged with the calories hash.

OMG! Took forever but finished two parts tonight. Also participated in the CodeNewbies Twitter chat and spoke to some developers on the CodeNewbie Slack chat. Networking is important!

and my progress so far at treehouse:

I wish I had finished that last one in Ruby Collections! Oh well. I will tomorrow as well as set a goal of completing the Ruby Loop part. Ideally i would love to finish these two tomorrow: