Day 21, Part Two: Hanging Out in the Treehouse and Learning Ruby the Hard Way

I am back and continuing with the Ruby track at Treehouse. Here is where I currently am:

Ruby Operators and Control Structures

In this course, you’ll learn about operators, which let you assign values to variables and test the value inside those variables. We’ll also explore control structures which let us control how our program runs, making our programs more efficient and intelligent. During the course, we’ll be writing simple programs that build on what you’ve learned in this and previous courses. You don’t need to have Ruby installed on your computer to follow along. You can follow along with the course by writing code in Workspaces.

And I just finished the “The Ruby If Statement” statement, and I am working on the If Challenge:

Set a variable called “too_fast” equal to “true” if the car_speed is faster than the speed_limit.

And now we move on to the else statement. Here is a code sample:

and the results:

Hi Jason!
treehouse:~/workspace$ ruby control_structures.rb
Enter name: Nick
That’s my name, too!
Would you like to see your name backwards? (yes or no)yes
This is your name backwards:
kciN
treehouse:~/workspace$ ruby control_structures.rb
Enter name: Meredith
Hi Meredith!
Would you like to see your name backwards? (yes or no)yes
This is your name backwards:
htidereM

By the way, the use of the downcase method is to ensure the answer is always lower case to better verify the input of yes.

And time for the Else challenge:

Set a variable called “too_fast” equal to “true” if the car_speed is faster than the speed_limit and “false” if the car_speed is less than the speed_limit.

And next is “The Ruby Elsif Statement”

Here is the code:

And the results:

treehouse:~/workspace$ ruby control_structures.rb
Enter name: Meredith
Hi Meredith!
Modify your name. Type ‘uppercase’ or ‘reverse’: reverse
This is your name backwards:
htidereM

treehouse:~/workspace$ ruby control_structures.rb
Enter name: Holly
Hi Holly!
Modify your name. Type ‘uppercase’ or ‘reverse’: uppercase
This is your name in all uppercase letters:
HOLLY

treehouse:~/workspace$ ruby control_structures.rb
Enter name: Zorro
Hi Zorro!
Modify your name. Type ‘uppercase’ or ‘reverse’: both
OK, here is your name reverse and uppercase!
ORROZ

And the elsif challenge:

Using an “elsif” statement, modify the code below to check whether or not the car_speed is equal to the speed_limit. If it is, set the variable “going_speed_limit” to true.

Next video is “The Ruby Case Statement”

Here is the code:

And the results:

treehouse:~/workspace$ ruby control_structures.rb
Enter name: Josiah
Hi Josiah!
Modify your name. Type ‘uppercase’ or ‘reverse’: both
OK, here is your name reverse and uppercase!
HAISOJ

treehouse:~/workspace$ ruby control_structures.rb
Enter name: Jebediah
Hi Jebediah!
Modify your name. Type ‘uppercase’ or ‘reverse’: reverse
This is your name backwards:
haidebeJ

treehouse:~/workspace$ ruby control_structures.rb
Enter name: Ezekial
Hi Ezekial!
Modify your name. Type ‘uppercase’ or ‘reverse’: uppercase
This is your name in all uppercase letters:
EZEKIAL

I miss doing the Learn Ruby the hard Way book, so i am going to mix these up. I am able to work with the TreeHouse program throughout the day, 5 minutes here or there. The Learn Ruby the Hard Way book is much more involved and I want to finish it.

So, in today’s Learn Ruby the Hard Way: Exercise 24: More Practice

The code:

And the result:

$ ruby ex24.rb
Let’s practice everything.
You’d need to know ’bout escapes with \ that do \n newlines and \t tabs.
__________________

The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explanation

where there is none.
__________________
This should be five: 5
With a starting point of 10000
we’d have 5000000 beans, 5000 jars, and 50 crates.

1. Make sure to do your checks: read it backward, read it out loud, and put comments above confusing parts.

Actually, I am pretty familiar with all of this. I actually like that in the Treehouse lessons I learned about the way Mr. Shaw wrapped the poem. Its all connected. We can go deeper.

2. Break the file on purpose, then run it to see what kinds of errors you get. Make sure you can fix it.

OK, I actually did this by mistake. I am trying out Aptana IDE and didn’t realize it had created an “end” to my “def” and I had two ends. The error was this:

Nick@WASHBUCKET /c/ruby
$ ruby ex24.rb
ex24.rb:35: syntax error, unexpected keyword_end, expecting end-of-input

The nice thing about Ruby is that the line number is right there. Then you go and look and have the eureka moment and can fix it. Or spend six hours crying because life isn’t fair.

Exercise 25: Even More Practice

And the results, as done in the IRB:

$ irb
DL is deprecated, please use Fiddle
irb(main):001:0> require “./ex25.rb”
=> true
irb(main):002:0> sentence = “All good things come to those who wait.”
=> “All good things come to those who wait.”
irb(main):003:0> words = Ex25.break_words(sentence)
=> [“All”, “good”, “things”, “come”, “to”, “those”, “who”, “wait.”]
irb(main):004:0> words
=> [“All”, “good”, “things”, “come”, “to”, “those”, “who”, “wait.”]
irb(main):005:0> sorted_words = Ex25.sort_words(words)
=> [“All”, “come”, “good”, “things”, “those”, “to”, “wait.”, “who”]
irb(main):006:0> sorted_words
=> [“All”, “come”, “good”, “things”, “those”, “to”, “wait.”, “who”]
irb(main):007:0> Ex25.print_first_word(words)
All
=> nil
irb(main):008:0> Ex25.print_last_word(words)
wait.
=> nil
irb(main):009:0> words
=> [“good”, “things”, “come”, “to”, “those”, “who”]
irb(main):010:0> Ex25.print_first_word(sorted_words)
All
=> nil
irb(main):011:0> Ex25.print_last_word(sorted_words)
who
=> nil
irb(main):012:0> sorted_words
=> [“come”, “good”, “things”, “those”, “to”, “wait.”]
irb(main):013:0> sorted_words = Ex25.sort_sentence(sentence)
=> [“All”, “come”, “good”, “things”, “those”, “to”, “wait.”, “who”]
irb(main):014:0> sorted_words
=> [“All”, “come”, “good”, “things”, “those”, “to”, “wait.”, “who”]
irb(main):015:0> Ex25.print_first_and_last(sentence)
All
wait.
=> nil
irb(main):016:0> Ex25.print_first_and_last_sorted(sentence)
All
who
=> nil

1. Take the remaining lines of the What You Should See output and figure out what they are doing. Make sure you understand how you are running your functions in the ex25 module.

I understand what is happening because I have done similar in the Learn Ruby on Rails and Treehouse lessons.

2. The Ex25 module doesn’t have to be in a file named ex25.rb. Try putting it in a new file with a random name, then import that file and see how you still have Ex25 available even though the file you made does not have ex25 in it.

It would not matter the name of the file as we call the module at the top.

3. Try breaking your file and see what it looks like in irb when you use it. You will have to quit irb with quit() to be able to reload it.

I did this, again, by mistake. Not sure still exactly what i did. I believe I had lost the name of the variable “words” and this screwed me up.

Nick@WASHBUCKET /c/ruby
$ irb
DL is deprecated, please use Fiddle
irb(main):001:0>
irb(main):001:0> require “./ex25.rb”
=> true
irb(main):002:0> sentence = “All good things come to those who wait.”
=> “All good things come to those who wait.”
irb(main):003:0> words = Ex25.break_words(sentence)
=> [“All”, “good”, “things”, “come”, “to”, “those”, “who”, “wait.”]
irb(main):004:0> words
=> [“All”, “good”, “things”, “come”, “to”, “those”, “who”, “wait.”]
irb(main):005:0> sorted_words = Ex25.sort_words(words)
=> [“All”, “come”, “good”, “things”, “those”, “to”, “wait.”, “who”]
irb(main):006:0> sorted_words
=> [“All”, “come”, “good”, “things”, “those”, “to”, “wait.”, “who”]
irb(main):007:0> Ex25.print_first_word(words)
NoMethodError: undefined method print_first_word' for Ex25:Module
from (irb):7
from c:/RailsInstaller/Ruby2.1.0/bin/irb:11:in


irb(main):008:0> Ex25.print_first_word(words)
NoMethodError: undefined method `print_first_word’ for Ex25:Module
from (irb):8

I exited and went through the commands line by line and everything worked. I also double-checked the function I was calling. I even retyped it, just in case.

OK, hail Mary time. I want to finish out the Treehouse “Ruby Operators and Control Structures” course!

First video: Multiple Conditions with && and ||

So, you use || for “or” and && for “and.”

and the results:

treehouse:~/workspace$ ruby favorite_number.rb
What is your favorite number? 3
That’s my favorite number!
treehouse:~/workspace$ ruby favorite_number.rb
What is your favorite number? 5
That’s my favorite number!
treehouse:~/workspace$ ruby favorite_number.rb
What is your favorite number? 42
That’s a pretty high even number!
treehouse:~/workspace$ ruby favorite_number.rb
What is your favorite number? 33
treehouse:~/workspace$ ruby favorite_number.rb
What is your favorite number? 33
That number is divisible by 3 and odd. Cool!

And the challenges:

Set the return value of the “check_speed” method to the string “safe” depending on the following condition:
The speed passed in as an argument is at least 40.
The speed passed in as an argument is less than or equal to 50.

You should use the “&&” logical operator to accomplish this task.

Modify the “valid_command?” method to return true when passed the following values: “y”, “yes”, “Y”, or “YES”.

And on to video “The Not Operator”

So, this discusses the use of the ! to negate a condition. Here is the code:

And the result:

treehouse:~/workspace$ ruby car.rb
Car 1 and car are not going the same speed.
Car 1 is going fast, but not as fast as car 2.

Then a quiz to check understanding and on to the next video, “Operator Precedence.”

The tag line is: “Ruby follows the same conventions as math! That means that operators have precedence over other operators and follow the order of operations.”

And this work in IRB shows this:

and a short quiz and on to the last video: “Practice time! Operators and Control Structures”

Here is the code:

and the results:

treehouse:~/workspace$ ruby number.rb
Enter your name: Nick
Hi Nick! That’s a great name.
What number would you like to test? 8953
Your number is not cleanly divisible by 3.
treehouse:~/workspace$ ruby number.rb
Enter your name: Nick
Hi Nick! That’s a great name.
What number would you like to test? 27
Your number is divisible by 3.

LAST QUIZ!

and I passed!

I got quite a bit done today!