Day 28: Part, Two: Some Treehouse

OK, its late and it was a long day but I am committed to continuing. Let’s get to it:

Video 1: Variables

Here is the code. Note that this is the same name.rb file from the last Ruby lesson at Treehouse. We also changed the way the puts statements at the end were written:

And the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby name.rb
Mr. Nick Teddy Queen

Challenge Task 1 of 4

In the initialize method of the Name class, set an instance variable called @title to the title argument. Note: you will have to write the initialize method.

Challenge Task 2 of 4

Below the Name class, instantiate a new Name instance set to the variable name with any title you choose.

Challenge Task 3 of 4

Inside the Name class, create a method called title that returns the @title variable.

Challenge Task 4 of 4

Call the title method on the name instance.

Video 2: Attribute Readers

The code:

Wow. That is a really neat code!

The result is the same:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby name.rb
Mr. Nick Teddy Queen

Challenge Task 1 of 1

In the Name class, create an attr_reader for the title instance variable.

Video 3: Attribute Writers and Accessors

First some work in IRB:

And having not been able to give myself a doctorate, we need to go into the file and fix this travesty!

OR, we can use attr_writer, and past that, attr_accessor which does reader and writer:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby name.rb
Mr. Nick Teddy Queen
Title: Mr.
Title: Dr.

Challenge Task 1 of 2

In the Name class, create an attr_writer for the first_name instance variable.

Challenge Task 2 of 2

In the Name class, create an attr_accessor for the last_name instance variable.

And that is it for tonight. I am halfway through this stage. Will finish tomorrow!