Day 29: Some Hard Way and Treehouse!

So, I plan to do Exercise 34 at Learn Ruby the Hard Way and then finish the stage I couldn’t finish last night at Treehouse. Let’s get started:

Exercise 34: Accessing Elements of Arrays

So, this is about accessing items in an array. Here is the array Mr. Shaw gives us:

and then a list to answer:

  1. The animal at 1.
  2. The third (3rd) animal.
  3. The first (1st) animal.
  4. The animal at 3.
  5. The fifth (5th) animal.
  6. The animal at 2.
  7. The sixth (6th) animal.
  8. The animal at 4.

So, I think I will check my answers by writing a script. First, however, let me pick which will be called:

  1. ruby
  2. peacock
  3. bear
  4. kangaroo
  5. whale
  6. peacock
  7. platypus
  8. whale

And then the code, which I wrote myself:

And the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex34.rb
bear
ruby
peacock
kangaroo
whale
platypus
So if bear is first, then we can figure out the rest!

And Mr. Shaw also want us to do this:

For each of these, write out a full sentence of the form: “The first (1st) animal is at 0 and is a bear.” Then say it backwards: “The animal at 0 is the 1st animal and is a bear.”

I will trust him.

  1. The animal at 1 is the ruby. It is the second animal in the list.
  2. The third animal in the list is peacock. It is at 2.
  3. The first animal is the bear. It is at 0.
  4. The animal at 3 is the kangaroo. It is the fourth animal.
  5. The fifth animal is the whale. It is 4.
  6. The animal at 2 is the peacock. It is third.
  7. The sixth animal is the platypus. It is at 5.
  8. The animal at 4 is the whale. It is at fifth.

And the study drills:

1. With what you know of the difference between these types of numbers, can you explain why the year 2010 in “January 1, 2010,” really is 2010 and not 2009? (Hint: you can’t pick years at random.)

Well, it is a string the way it is written. I also wonder if it has something to do with Ruby sees years. Not sure, however.

2. Write some more arrays and work out similar indexes until you can translate them.

I mean, I think I have it but here is one more in code:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby studydrill34.rb
Mad Max Fury Road
Ghostbusters
Casablanca
Avengers
Star Wars
Big Trouble in Little China
While right now the first movie in the list is Mad Max Fury Road, Big Trouble in Little China is definitely not last in my book.

3. Use Ruby to check your answers.

DONE!

Now on to Treehouse.

First video: Methods

and the results:

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

Challenge Task 1 of 1

In the Name class, create a method called full_name that returns a string of the first_name and last_name with a space in between.

Next video: Instance Variables and Local Variables

The code:

And the results in IRB:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ irb -r “./name.rb”
Mr. Nick Teddy Queen
Ms. Meredith Page Queen
>> name = Name.new(“Mr.”, “Nick”, “Teddy”, “Queen”)
=> #
>> name.full_name
=> “Nick Teddy Queen”
>> name.first_and_middle_name
=> “Nick Teddy”

And last video: The to_s method

First code:

and results:

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

And the second code, after overriding the to_s method:

And a look at the .inspect method:

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

Challenge Task 1 of 1

Create a to_s method for Name that returns the first_name and last_name separated by a space.

All done for tonight!