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:

1 2 3 4 5 6 7 |
puts "How many hours are in a week?" puts "Well, there are 24 hours in a day, and 7 days in a week... so it is #{24 * 7}." puts "Wow, that's a lot! How about in a year?" puts "You need to do that math yourself! If there are 365 days in a year, with 24 hours a day, that would be #{24 * 365}." |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# This line prints the statement on the screen. puts "I will now count my chickens:" # This line adds 25 to 5, which is 30 divided by 6 puts "Hens #{25.0 + 30.0 / 6.0}" # This line was hard. I had to look it up! I forgot the # order of operations. In any case, we first take 25 multiplied # by 3 and get 75. You then take 4 percent of 75, getting 3. # Finally, you substract 3 from 100, getting 97! # Prints the statement on the screen. puts "Roosters #{100.0 - 25.0 * 3.0 % 4.0}" # OK, again we do the order of operations. I am stumped. # I will need to work through this one a bit more. In any case, Google # tells me the answer is 6.75. Did Ruby round up or did Google screw up? puts 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0 # This line prints the statement on the screen. puts "Is it true that 3.0 + 2.0 < 5.0 - 7.0?" # OK, so here Ruby evaluated the question of whether 3 + 2 is less than 5 - 7. It says false. puts 3.0 + 2.0 < 5.0 - 7.0 # So, the next two lines print the question on the screen and then does the math for each. puts "What is 3.0 + 2.0? #{3.0 + 2.0}" puts "What is 5.0 - 7.0? #{5.0 - 7.0}" # These lines print the statements to the screen. puts "Oh, that's why it's false." puts "How about some more?" # And these lines evaluate each set of questions, the first being is 5 greater than -2; # the next is 5 greater than or equal to -2; and the last is 5 less than or equal to -2. puts "Is it greater? #{5.0 > -2.0}" puts "Is it greater or equal? #{5.0 >= -2.0}" puts "Is it less or equal? #{5.0 <= -2.0}" |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
cars = 100 space_in_a_car = 4.0 drivers = 30 passengers = 90 cars_not_driven = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * space_in_a_car average_passengers_per_car = passengers / cars_driven puts "There are #{cars} cars available." puts "There are only #{drivers} drivers available." puts "There will be #{cars_not_driven} empty cars today." puts "We can transport #{carpool_capacity} people today." puts "We have #{passengers} to carpool today." puts "We need to put about #{average_passengers_per_car} in each car." |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# These lines define the variables. First, how many of each... cars = 100 space_in_a_car = 4 drivers = 30 passengers = 90 # And then some calculations to figure out some other necessary variables... # To get cars driven, we take the number of cars and subtract the number of drivers. cars_not_driven = cars - drivers # The number of cars is found by looking at the how many drivers we have. cars_driven = drivers # We find out how many are able to carpool by multiplying the numbers of cars being driven by the avaliable space in a car. carpool_capacity = cars_driven * space_in_a_car # And we find out the average passengers by dividing the passengers by the number of cars being driven. average_passengers_per_car = passengers / cars_driven # And then we need to give the info to the end user. The script runs, and we have the answers we need! puts "There are #{cars} cars available." puts "There are only #{drivers} drivers available." puts "There will be #{cars_not_driven} empty cars today." puts "We can transport #{carpool_capacity} people today." puts "We have #{passengers} to carpool today." puts "We need to put about #{average_passengers_per_car} in each car." |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
?> i = 45 => 45 >> v = 65 => 65 >> i < v => true >> i <= v => true >> i > v => false >> i * v => 2925 >> i / v => 0 >> i = 45.0 => 45.0 >> v = 65.0 => 65.0 >> i / v => 0.6923076923076923 |

**Exercise 5: More Variables and Printing**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
my_name = 'Zed A. Shaw' my_age = 35 # not a lie in 2009 my_height = 74 # inches my_weight = 180 # lbs my_eyes = 'Blue' my_teeth = 'White' my_hair = 'Brown' puts "Let's talk about #{my_name}." puts "He's #{my_height} inches tall." puts "He's #{my_weight} pounds heavy." puts "Actually that's not too heavy." puts "He's got #{my_eyes} eyes and #{my_hair} hair." puts "His teeth are usually #{my_teeth} depending on the coffee." # this line is tricky, try to get it exactly right puts "If I add #{my_age}, #{my_height}, and #{my_weight} I get #{my_age + my_height + my_weight}." |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
name = 'Zed A. Shaw' age = 35 # not a lie in 2009 height = 74 # inches weight = 180 # lbs eyes = 'Blue' teeth = 'White' hair = 'Brown' puts "Let's talk about #{name}." puts "He's #{height} inches tall." puts "He's #{weight} pounds heavy." puts "Actually that's not too heavy." puts "He's got #{eyes} eyes and #{hair} hair." puts "His teeth are usually #{teeth} depending on the coffee." # this line is tricky, try to get it exactly right puts "If I add #{age}, #{height}, and #{weight} I get #{age + height + weight}." |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
name = 'Zed A. Shaw' age = 35 # not a lie in 2009 height = 74 # inches weight = 180 # lbs eyes = 'Blue' teeth = 'White' hair = 'Brown' puts "Let's talk about #{name}." puts "He's #{height} inches tall." puts "He's #{weight} pounds heavy." puts "Actually that's not too heavy." puts "He's got #{eyes} eyes and #{hair} hair." puts "His teeth are usually #{teeth} depending on the coffee." # this line is tricky, try to get it exactly right puts "If I add #{age}, #{height}, and #{weight} I get #{age + height + weight}." puts "I am #{height * 2.54} cm tall." puts "And I weigh #{weight * 0.453592} kg." |

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!

1 |
puts "Or in merry ole' England, #{weight * 0.0714286} stones!" |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
types_of_people = 10 x = "There are #{types_of_people} types of people." binary = "binary" do_not = "don't" y = "Those who know #{binary} and those who #{do_not}." puts x puts y puts "I said #{x}." puts "I also said: '#{y}'." hilarious = false joke_evaluation = "Isn't that joke so funny?! #{hilarious}" puts joke_evaluation w = "This is the left side of..." e = "a string with a right side." puts w + e |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# Setting a variable of 10 for types_of_people types_of_people = 10 # setting a variable for x x = "There are #{types_of_people} types of people." # setting binary to binary... binary = "binary" # setting don't as a variable for do_not do_not = "don't" # and setting a variable for y, but inside this it calls for the variables for binary and do_not y = "Those who know #{binary} and those who #{do_not}." # prints to the screen x and then y puts x puts y # each line prints to the string and calls the x variable puts "I said #{x}." # and then the y variable... puts "I also said: '#{y}'." # sets false as the variable for hilarious hilarious = false # adds a variable for joke_evaluation, which also calls a varibale for hilarious inside it. joke_evaluation = "Isn't that joke so funny?! #{hilarious}" # prints joke_evaluation to the screen puts joke_evaluation # sets variables for w and e... w = "This is the left side of..." e = "a string with a right side." # combines the two variables together doing math! puts w + e |

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!