Day 15: Still Learning Ruby the Hard Way, Exercises 4 to 6

OK, I am back and ready to rock out the Zed Shaw’s Learn Ruby the Hard Way book. We left off with me looking at this:

3. Find something you need to calculate and write a new .rb file that does it.

It was late. I was tired. So, I decided to sit down and think of something I could code. I figured I could follow the simple example Mr. Shaw made and look at the number of hours in a week and a year. Here is my code:

and here is what it does:

PS C:\ruby> ruby studydrill3.rb
How many hours are in a week?
Well, there are 24 hours in a day, and 7 days in a week… so it is 168.
Wow, that’s a lot! How about in a year?
You need to do that math yourself! If there are 365 days in a year, with 24 hours a day, that would be 8760.

OK, it is simple, but its mine!

So, the last of the study drill discusses the idea of the math being “wrong” due to there being no fractions, only whole numbers. I noticed that yesterday. So, to get fractions we need to use something called a “floating point” number. I had to rewrite the ex3.rb file with this:

and here is the result:

PS C:\ruby> ruby ex3.rb
I will now count my chickens:
Hens 30.0
Roosters 97.0
6.75
Is it true that 3.0 + 2.0 < 5.0 - 7.0? false What is 3.0 + 2.0? 5.0 What is 5.0 - 7.0? -2.0 Oh, that's why it's false. How about some more? Is it greater? true Is it greater or equal? true Is it less or equal? false

That 6.75 haunted me. I am not a huge math fan. I respect it and its power, but I was always a literature and history fan.

Exercise 4: Variables And Names

So, what is a variable? Its a name for something.

Here is what I typed:

And the result:

PS C:\ruby> ruby ex4.rb
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.

OK, on to the study drills.

No number, but Mr. Shaw had an error the first time he wrote the program. It was on line 14 and looks like he wrote a variable wrong.

1. I used 4.0 for space_in_a_car, but is that necessary? What happens if it’s just 4?

and, associated with it:

2. Remember that 4.0 is a floating point number. It’s just a number with a decimal point, and you need 4.0 instead of just 4 so that it is floating point.

I tried this. Here are the results I got:

PS C:\ruby> ruby ex4.rb
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120 people today.
We have 90 to carpool today.
We need to put about 3 in each car.

I think the issue here is that without the floating point the problem would round up. The floating point adds accuracy…

3. Write comments above each of the variable assignments.

6. Try running ruby from the Terminal as a calculator like you did before and use variable names to do your calculations. Popular variable names are also i, x, and j.

Exercise 5: More Variables and Printing

And the results:

PS C:\ruby> ruby ex5.rb
Let’s talk about Zed A. Shaw.
He’s 74 inches tall.
He’s 180 pounds heavy.
Actually that’s not too heavy.
He’s got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.

1. Change all the variables so there is no my_ in front of each one. Make sure you change the name everywhere, not just where you used = to set them.

2. Try to write some variables that convert the inches and pounds to centimeters and kilograms. Do not just type in the measurements. Work out the math in Ruby.

I really liked doing this. I had to think and do some research. I had no idea what the conversions were! I got carried away and added one more line!

And the result:

PS C:\ruby> ruby ex5.rb
Let’s talk about Zed A. Shaw.
He’s 74 inches tall.
He’s 180 pounds heavy.
Actually that’s not too heavy.
He’s got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
I am 187.96 cm tall.
And I weigh 81.64656 kg.
Or in merry ole’ England, 12.857147999999999 stones!

Exercise 6: Strings and Text

Continuing on with strings. Here is what I typed:

PS C:\ruby> ruby ex6.rb
There are 10 types of people.
Those who know binary and those who don’t.
I said There are 10 types of people..
I also said: ‘Those who know binary and those who don’t.’.
Isn’t that joke so funny?! false
This is the left side of…a string with a right side.

1. Go through this program and write a comment above each line explaining it.

2. Find all the places where a string is put inside a string. There are four places. and 3. Are you sure there are only four places? How do you know? Maybe I like lying.

I think I found 6.

4. Explain why adding the two strings w and e with + makes a longer string.

I learned this earlier! Its a string concatenation. Basically, Ruby combines the strings together.

5. What happens when you change the strings to use ‘ (single-quote) instead of ” (double-quote)? Do they still work? Try to guess why.

So, I went and made the change. Here are the results one after another. Notice the missing part!

PS C:\ruby> ruby ex6.rb
There are 10 types of people.
Those who know binary and those who don’t.
I said There are 10 types of people..
I also said: ‘Those who know binary and those who don’t.’.
Isn’t that joke so funny?! false
This is the left side of…a string with a right side.

PS C:\ruby> ruby ex6.rb
There are 10 types of people.
Those who know binary and those who don’t.
I said There are 10 types of people..
I also said:
Isn’t that joke so funny?! false
This is the left side of…a string with a right side.

OK, that is enough for today! I will return tomorrow to continue onward!