Day 25: Continuing to Learn Ruby the Hard Way in a Treehouse

I try to keep the titles interesting, but I am running out of ways to do such.

FYI, I had a lot of false starts. First time I sat down we ended up having a tornado warning where I live. Next time I finally settle down and my dog demands me take him for a walk. Thankfully, the rain had stopped. But when we got to the farthest reaches of the walk it begins a torrential downpour and soaked me completely.

During all of this I did finish Exercise 27: Memorizing Logic by creating flash cards in AnkiDroid. I will be studying them this week. So, on to Exercise 28: Boolean Practice.

In this lesson I have to guess what the answer to each of the posed Boolean problems will be and then run them. So, I will put a comment next to each and then see how I do!

Here are my guesses with the code:

and then I opened IRB and ran each. I did well and got them all. The last ones were mind-bending, but if you take your time they are doable.

demosthenes131@rails-tutorial:~/workspace $ irb
>> true && true
=> true
>> false && true
=> false
>> 1 == 1 && 2 == 1
=> false
>> “test” == “test”
=> true
>> 1 == 1 || 2 != 1
=> true
>> true && 1 == 1
=> true
>> false && 1 == 1
=> false
>> true || 1 == 1
=> true
>> “test” == “testing”
=> false
>> 1 != 0 && 2 == 1
=> false
>> “test” != “testing”
=> true
>> “test” == 1
=> false
>> !(true && false)
=> true
>> !(1 == 1 && 0 != 1)
=> false
>> !(10 == 1 || 1000 == 1000)
=> false
>> !(1 != 10 || 3 == 4)
=> false
>> !(“testing” == “testing” && “Zed” == “Cool Guy”)
=> true
>> 1 == 1 && (!(“testing” == 1 || 1 ==0))
=> true
>> “chunky” == “bacon” && (!(3 == 4 || 3 == 3))
=> false
>> 3 == 3 && (!(“testing” == “testing” || “Ruby” == “Fun”))
=> false

Study drill time!

1. and 2. here are a lot of operators in Ruby similar to != and ==. Try to find as many “equality operators” as you can. They should be like < or <=. Write out the names of each of these equality operators. For example, I call != "not equal."

Done this before. You have the following:

> Greater than
>= Greater than or equal to
< Less than <= Less than or equal to == Equal === Used in a case/ when control structure which we did in Treehouse a bit back && and || or ! not != not equal

3. Play with the Ruby by typing out new boolean operators, and before you press Enter try to shout out what it is. Do not think about it. Shout the first thing that comes to mind. Write it down, then press Enter, and keep track of how many you get right and wrong.

With my Treehouse experience I have been doing this a lot so I’m moving forward!

Exercise 29: What If

So, the nice thing is I have done this at the Hartl tutorial and the Treehouse lesson. The reinforcement in very important!

Here’s the code:

and the results:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex29.rb
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.

And on to the study drills:

1. What do you think the if does to the code under it?

It asks Ruby to evaluate the problem asked of it, basically if the following is true, do this.

2. and 3. Why does the code under the if need to be indented two spaces? and What happens if it isn’t indented?

I am still getting used to the proper formatting, but basically, the indenting makes it readable to humans. Ruby will still run it the same, but the indenting makes the programmers life easier.

4. Can you put other boolean expressions from Exercise 27 in the if-statement? Try it.

Yep. For example:

Returns:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex29.rb
Too many cats! The world is doomed!
Not many cats! The world is saved!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.
demosthenes131@rails-tutorial:~/workspace/LRTHW $

5. What happens if you change the initial values for people, cats, and dogs?

It would change the outputs of the script. Here is an example:

and the results:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex29.rb
Not many cats! The world is saved!
The world is drooled on!
People are less than or equal to dogs.

And now on to Treehouse.

First video “Iteration With Each”

So, first we run this:

and get this:

treehouse:~/workspace$ ruby each.rb
The current item is 0.
The current item is 1.
The current item is 2.
The current item is 3.
The current item is 4.
The current item is 5.

Then we comment out a portion and write an each statement, like so:

and get the same results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby each.rb
The current array item is: 0
The current array item is: 1
The current array item is: 2
The current array item is: 3
The current array item is: 4
The current array item is: 5

And we create a second set of code to show using the each method and addition:

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby each_addition.rb
The current item + 2 is 2.
The current item + 2 is 3.
The current item + 2 is 4.
The current item + 2 is 5.
The current item + 2 is 6.
The current item + 2 is 7.

Challenge Task 1 of 1

Using the each method, iterate over every item in the numbers array and print the item using the puts method.

Next video is “Hash Iteration.”

Here is the code:

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby hash_iteration.rb
The hash key is name and the value is Treehouse.
The hash key is location and the value is Portland, OR.
Key: name
Key: location
Value: Treehouse
Value: Portland, OR

Challenge Task 1 of 1

Using the each method, iterate over every item in the contact hash and print the key and value using the puts method.

And onward to “Times Iteration.”

Here is the first bit of code:

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby times.rb
Hello!
Hello!
Hello!
Hello!
Hello!

and the second bit of code, using the times method with arguments:

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby times_with_arguments.rb
Hello! 0
Hello! 1
Hello! 2
Hello! 3
Hello! 4

Challenge Task 1 of 1

Print anything to standard output using the puts method 5 times by calling the times method on the number 5.

and the last video tonight, “For Loops”

The code:

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby for_loop.rb
The current item is 1.
The current item is 2.
The current item is 3.
The current item is 4.
The current item is 5.
The current item is 6.
The current item is 7.
The current item is 8.
The current item is 9.
The current item is 10.
The current item is Programming.
The current item is is.
The current item is fun.

Challenge Task 1 of 1

Use a for loop to print each item in the animals array to the screen using the puts method.

and done!

And I am finished for tonight!