Day 32: Finishing Ruby Track in Treehouse

I have a lofty goal today. I want to finish the Ruby track at Treehouse. I did work yesterday (which technically should be day 32) and completed the True and False video and quiz as well as the Nil video and challenge. I did these over my phone. That means I am starting on the Precedence video today. I am going to work hard and get through this. Let’s go:

and finish the quiz!

Next video: Conditional Assignment

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby conditional_assignment.rb
Nick

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby conditional_assignment.rb
Andrew

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby conditional_assignment.rb
Andrew

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby conditional_assignment.rb
Nick

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby conditional_assignment.rb
Tom Sawyer

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby conditional_assignment.rb
Nick

Challenge Task 1 of 1

Using the conditional assignment operator, create a variable called name that is conditionally set to a string containing your name.

Booyah! Next! Build a Simple Todo List Program

And a quick overview of how close I am:

First Video: Part 1: Write the Classes

The code, in two different files:

and

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby todo_list.rb
#
#

Challenges:

Challenge Task 1 of 2

Create a class called TodoList. It is not necessary to define an initialize method yet.

Challenge Task 2 of 2

Create another class called TodoItem. It is not necessary to define an initialize method yet.

Video 2: Part 2: Todo Item Methods

and a few tests:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby todo_list.rb
[I] Milk

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby todo_list.rb
[I] Milk
[C] Milk

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby todo_list.rb
[I] Milk
[C] Milk
[I] Milk

Challenge Task 1 of 2

In the initialize method of TodoItem, add an instance variable called @complete. Set the @complete variable to false upon instantiation.

Challenge Task 2 of 2

Create a method in the TodoItem class called complete? which returns the @complete instance variable.

Next video: Part 3: Adding Todo Items

Code:

and results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby todo_list.rb
#, #]>

Challenge Task 1 of 4

In the initialize method of the TodoList class, create an instance variable called @todo_items. Set that instance variable to be an empty array.

Challenge Task 2 of 4

Using the attr_reader method, add an attribute reader for todo_items in the TodoList class.

Challenge Task 3 of 4

Define a method in the TodoList class called add_item which takes one argument. Don’t worry about adding anything to the method body just yet.

Challenge Task 4 of 4

In the add_item method of the TodoList class, create a new TodoItem from the argument passed in and append it to the todo_items array.
Assume that the TodoItem class is already defined and takes one argument upon initialization.

Video 4: Part 4: Removing Todo Items

Code:

Results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby todo_list.rb
#, #]>
Eggs were removed from the list.
#]>

Challenge Task 1 of 1

In the TodoList class, fill in the empty? method to return a boolean value.
The empty? method should return true if there are no elements in the @todo_items array.
The method should return false if there is more than one element in the @todo_items array.

Video 5 Part 5: Marking Todo Items Complete

Code:

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby todo_list.rb
Eggs were removed from the list.
Milk was marked as complete.
#, #]>

Challenge Task 1 of 1

Fill in the find_index method to return the index of a todo item in the @todo_items array given the name.
The method should return the index of the item if the item is found and nil if it is not found.

Last video! Part 6: Printing Todo Lists

Code:

Results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby todo_list.rb
Eggs were removed from the list.
Milk was marked as complete.
Groceries List – all items
——————————
[C] Milk
[I] Bread
[I] Butter

Groceries List – complete items
——————————
[C] Milk

Groceries List – incomplete items
——————————
[I] Bread
[I] Butter

Challenge Task 1 of 1

In the TodoList class, fill in the contains? method so that it returns a boolean value if the todo items array contains an item with the name argument.

On to Ruby Blocks!

First video: What Are Blocks?

And this video goes in to explaining Ruby blocks.

Some code examples:

and Challenge Task 1 of 1

Using the times method, write a block that outputs “Hello World!” to the screen 3 times.

Hmm, the same as the video… I’ll use another convention…

Video 2: Write a Block Method

First bit of code:

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby block_method.rb
This is the first line in block_method.

More:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby block_method.rb
This is the first line in block_method.
This statement is called from the block.

And even more:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby block_method.rb
This is the first line in block_method.
This statement is called from the block.
This statement is after the yield keyword.

And again:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby block_method.rb
This is the first line in block_method.
This statement is called from the block.
This statement is called from the block.
This statement is after the yield keyword.

And even more!

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby block_method.rb
This is the first line in block_method.
This statement is called from the block.
This statement is called from the block.
This statement is after the yield keyword.
This is the first line in block_method.
block_method.rb:3:in block_method': no block given (yield) (LocalJumpError)
from block_method.rb:12:in

This demonstrates that a block is required.

Challenge Task 1 of 1

Call the method block_method using a block and print anything out inside of it using a puts statement.

def block_method
puts “This is the first line in the block method.”
yield
end

block_method do
puts “Witness me!”
end

Next video: Block Arguments

First, a refresher of the .each method:

and then a code example:

and result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby block_arguments.rb
Enter your name: Nick
That’s a cool name, Nick!

and we can see that we can assign whatever variable for the arguments:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby block_arguments.rb
Enter your name: Tom
That’s a cool name, Tom!

And a bit more:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby block_arguments.rb
Enter your name: Nick
That’s a cool name, Nick!
my_name:

and with an implicit return of name we get the result we want:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby block_arguments.rb
Enter your name: Nick
That’s a cool name, Nick!
my_name: Nick

Challenge Task 1 of 1

Call the method get_name with a block. Assign the block argument a variable called name. Do at least one thing in the block such as printing out the name.

Last video in this stage: Calling Blocks

Some initial code:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby calling_blocks.rb
Enter your name: Nick
That’s a cool name, Nick!
my_name: Nick

And another:

And the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby calling_blocks.rb
Enter your name: Kit
That’s a cool name, Kit!
my_name: Kit

And adding another argument:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby calling_blocks.rb
Enter your name: Nick
Age: 36
That’s a cool name, Nick! Age 36.
my_name: Nick

And same using yield instead of block.call:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby calling_blocks.rb
Enter your name: Nick
Age: 36
That’s a cool name, Nick! Age 36.
my_name: Nick

And using block.given?

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby calling_blocks.rb
Enter your name: Nick
Age: 43
my_name: Nick

And some fancy stuff:

With block:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby calling_blocks.rb
Enter your name: Nick
Age: 45
That’s a cool name, Nick! Age 45.
my_name:

Without block:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby calling_blocks.rb
No block given.
my_name:

Challenge Task 1 of 1

Using the call method, modify the print_header method to call the block sent in to it.

Next stage!

Blocks in the Wild: Strings and Integers

And some examples of different methods using blocks:

Challenge Task 1 of 1

Using the each_char method, print out every character in the variable string with a “-” on either side.

Video 2: Blocks in the Wild: Arrays and Hashes

More work in IRB:

Challenge Task 1 of 1

Using the select method, create a new array named house that contains any items from the array variable with a length greater than four characters.

Next video: Build a Simple Benchmarker

The code:

and then result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby simple_benchmarker.rb
Elapsed time: 8.907e-06

and then the final code, with some results following as the app was updated:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby simple_benchmarker.rb
Elapsed time: 2.672672349
demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby simple_benchmarker.rb
…..Elapsed time: 3.293355817 seconds
demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby simple_benchmarker.rb
…..
Sleep a random amount of time results
Elapsed time: 1.940551037 seconds

Challenge Task 1 of 2

Shown below is the SimpleBenchmarker class. Instantiate a variable named benchmarker which is an instance of the SimpleBenchmarker class.

Challenge Task 2 of 2

Call the run method on the benchmarker object and pass it two arguments. The first argument should be a string of the description. The second argument can be a block with any code you choose.

Last video this stage: Implementing Block Methods

The code:

And the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby my_array.rb
item: 1
item: 2
item: 3

Challenge Task 1 of 1

There’s a bug in the MyArray class! Modify the each method to return the member array.

Last stage in the Ruby Blocks!

Build a Monster Class: Part 1

The code:

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby monster.rb
Fluffy says… Welcome to my home.

Challenge Task 1 of 3

Create a class named Monster.

Challenge Task 2 of 3

Create an initialize method for the Monster class that takes an argument and sets an instance variable called @name.

Challenge Task 3 of 3

In the initialize method of the Monster class, create an instance variable called @actions that is set to an empty hash.

Video 2: Build a Monster Class: Part 2

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby monster.rb
Fluffy says… Welcome to my home.
{:screams=>0, :scares=>0, :runs=>0, :hides=>0}
Fluffy screams! BOO!
Fluffy scares you! Go Away!
Fluffy runs! Going to get you!
Fluffy hides! Running away and hiding!
{:screams=>1, :scares=>1, :runs=>1, :hides=>1}

Challenge Task 1 of 1

Inside of the Monster class, modify the say method to yield to the block.

Third video: Build a Monster Class: Part 3

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby monster.rb
Fluffy says… Welcome to my home.
{:screams=>0, :scares=>0, :runs=>0, :hides=>0}
Fluffy screams! BOO!
Fluffy scares you! Go Away!
Fluffy runs! Going to get you!
Fluffy hides! Running away and hiding!

——————————
Fluffy scoreboard
——————————
– Screams: 1
– Scares: 1
– Runs: 1
– Hides: 1
——————————

Challenge Task 1 of 2

Modify the scream method to increment the screams key of the @actions hash before printing out that the monster screams.

Challenge Task 2 of 2

After the print statement in the scream method, yield to the block.

Last video: Build a Monster Class: Part 4

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Ruby Blocks $ ruby monster.rb
Fluffy says… Welcome to my home.
{:screams=>0, :scares=>0, :runs=>0, :hides=>0}
Fluffy screams! BOO!
Fluffy scares you! Go Away!
Fluffy runs! Going to get you!
Fluffy hides! Running away and hiding!
1, :scares=>1, :runs=>1, :hides=>1}>

——————————
Fluffy scoreboard
——————————
– Screams: 1
– Scares: 1
– Runs: 1
– Hides: 1
——————————

Challenge Task 1 of 1

In the scream method, modify the yield statement to send the monster instance to a block if a block is sent in to the scream method.

Last stage of the track!

I am almost there. Last stage: Build an Address Book in Ruby

First video: Contact Class: Part 1

And the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby contact.rb
Nick Teddy Queen
Jason Seifer

Challenge Task 1 of 1

Create a class called Contact.

Video 2: Contact Class: Part 2

Code:

and results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby contact.rb
Jason Seifer
Nick Teddy Queen
Seifer, Jason
Queen, Nick T.

Challenge Task 1 of 1

Create a method called full_name that returns a string of the first_name and last_name attributes separated by a space.

Next video: Phone Number Class

The code:

require “./phone_number”

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby contact.rb
print_phone_numbers
Phone Numbers
Home: 123-456-7890
Work: 987-654-3210

Challenge Task 1 of 1

Create a to_s method on the PhoneNumber class that displays the kind variable, followed by a colon (:), followed by a space, and then the number variable.

OK, two videos in a row: Address Class: Part 1 and 2

First video code:

and result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address.rb
Home: 123 Main St., Portland, OR, 12345

123 Main St.
Portland, OR, 12345

Second video code:

and results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby contact.rb
Jason Seifer
Phone Numbers
Home: 123-456-7890
Work: 456-789-0123
Addresses
Home: 123 Main St. , Portland, OR, 12345

Challenge Task 1 of 2

Below the Address class definition, initialize a variable called address which will be a new instance of the Address class. Double check to make sure all required arguments are given.

Challenge Task 2 of 2

Call the to_s method on the address variable.

Last video in this stage: Address Book Class

The code:

require “./contact”

And the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rb
Contact List
Seifer, Jason
Queen, Nick

Challenge Task 1 of 2

Create a class called AddressBook.

Challenge Task 2 of 2

In the initialize method of the address book class, define an instance variable called contacts which is set to an empty array.

On to the next stage: Search

First video: Searching By Name

And the code:

and the first results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rb
Name search results (nick)
Nick Queen
Phone Numbers
Home: 555-555-5555
Addresses
Home: 123 Main St. , Baltimore, MD, 12345

And a small change to the search parameters:

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rb
Name search results (n)
Jason Seifer
Phone Numbers
Home: 123-456-7890
Work: 456-789-0123
Addresses
Home: 123 Main St. , Portland, OR, 12345

Nick Queen
Phone Numbers
Home: 555-555-5555
Addresses
Home: 123 Main St. , Baltimore, MD, 12345

And another:

Which has no results…

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rbName search results (e)

So time to add in an or statement to include last names…

and it works:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rb
Name search results (e)
Jason Seifer
Phone Numbers
Home: 123-456-7890
Work: 456-789-0123
Addresses
Home: 123 Main St. , Portland, OR, 12345

Nick Queen
Phone Numbers
Home: 555-555-5555
Addresses
Home: 123 Main St. , Baltimore, MD, 12345

And one last change to do the full_name:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rb
Name search results (e)
Jason Seifer
Phone Numbers
Home: 123-456-7890
Work: 456-789-0123
Addresses
Home: 123 Main St. , Portland, OR, 12345

Nick Queen
Phone Numbers
Home: 555-555-5555
Addresses
Home: 123 Main St. , Baltimore, MD, 12345

Challenge Task 1 of 2

Initialize a variable called address_book set to a new instance of the AddressBook class.

Challenge Task 2 of 2

Append the contact variable to the contacts array inside of the address_book variable.

Next video: Searching by Phone Number

The code:

and results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rb
Phone search results (555)
Nick Queen
Phone Numbers
Home: 555-555-5555
Addresses
Home: 123 Main St. , Baltimore, MD, 12345

and results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rb
Phone search results (5)
Jason Seifer
Phone Numbers
Home: 123-456-7890
Work: 456-789-0123
Addresses
Home: 123 Main St. , Portland, OR, 12345

Jason Seifer
Phone Numbers
Home: 123-456-7890
Work: 456-789-0123
Addresses
Home: 123 Main St. , Portland, OR, 12345

Nick Queen
Phone Numbers
Home: 555-555-5555
Addresses
Home: 123 Main St. , Baltimore, MD, 12345

and a little cleanup of the print_results code and de-duplication of the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rb
Name search results (n)
Jason Seifer
Phone Numbers
Home: 123-456-7890
Work: 456-789-0123
Addresses
Home: 123 Main St. , Portland, OR, 12345

Nick Queen
Phone Numbers
Home: 555-555-5555
Addresses
Home: 123 Main St. , Baltimore, MD, 12345

Phone search results (5)
Jason Seifer
Phone Numbers
Home: 123-456-7890
Work: 456-789-0123
Addresses
Home: 123 Main St. , Portland, OR, 12345

Nick Queen
Phone Numbers
Home: 555-555-5555
Addresses
Home: 123 Main St. , Baltimore, MD, 12345

Challenge Task 1 of 1

Call the find_by_phone_number method on the address_book instance and pass it any phone number.

Last video this stage: Searching By Address

The code:

and results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse/Build an Address Book in Ruby (master) $ ruby address_book.rb
Address search results (md)
Nick Queen
Phone Numbers
Home: 555-555-5555
Addresses
Home: 123 Main St. , Baltimore, MD, 12345

Challenge Task 1 of 1

Call the find_by_address method on the address_book instance and send it any address.

It’s finally here. The last stage of the Learn Ruby track… Input and Output

First video: Displaying a Menu