Return of Day 27: Back in the Treehouse

So, I have a little more time and decided to work on a little Treehouse.

Video 1: Ruby Classes

And this is just an overview of the idea of Ruby classes, which I covered in Michael Hartl’s tutorial.

Video 2: Instantiation

OK, this goes a little deeper. We discuss creating new objects and the difference between classes and instances. The act of creating an instance of a class is called instantiation. Once created, that instance is called an object. Take a look at the irb session below:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ irb
>> string = String.new
=> “”
>> string.class
=> String
>> nick = String.new(“Nick”)
=> “Nick”
>> Array.new
=> []
>> Hash.new
=> {}
>> exit

And then some code:

And the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby myclass.rb
This is the initialize method.

And the challenge:

Challenge Task 1 of 2

Instantiate a new instance of the String class and assign the newly instantiated instance to the variable string.

Challenge Task 2 of 2

Instantiate a new instance of the Array class and assign the newly instantiated instance to the variable array.

Video 3: Ruby Objects

So, object oriented programming. I have heard of it a lot but this was the first direct discussion of it.

Challenge Task 1 of 1

Using the respond_to? method, ask the string variable if it responds to the upcase method. Remember, the respond_to? method can take a string or a symbol as an argument.

Video 4: Creating a Class

The one thing I can say I like about TreeHouse is that it gives the lessons in nice, bite-sized chunks but that they are also easy to follow, even when really difficult stuff. This isn’t necessarily difficult, and it may be because its my second time doing this, but I am beginning to understand the idea of creating a class.

The code:

So, we create a class named “Name” and then defined three methods underneath it, and then called them.

This is the location in Ruby of what we created:

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

and here we run it and call each method:

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

Challenge Task 1 of 3

Instantiate an instance of the Name class and assign it to the variable name. This must come after the definition of the Name class.

Challenge Task 2 of 3

Call the puts method with the name variable’s first_name method as an argument.

Challenge Task 3 of 3

On the next line, call the puts method with the name variable’s last_name method as an argument.

Another badge!