For the first exercise, Zed has us saying out loud what everything is. It helps to know what these things are so when you mention a "modulus" character to another programmer, or them to you, you know what it is :).
And here we go...
+ plus [performs addition]
- minus [performs subtraction]
/ slash [performs division]
* asterisk [performs multiplication]
% percent [provides the remainder of a division, i.e. the modulus]
< less-than [what's on the left is less than what's on the right]
> greater-than [what's on the left is greater that than what's on the right]
<= less-than-equal [what's on the left is less than or equal to what's on the right]
>= greater-than-equal [what's on the left is greater than or equal to what's on the right]
The first thing to do was to tell what the operations were, fairly straightforward [see above]
Next, let's get gedit and ruby in to the game:
1 puts "I will now count my chickens:"
3 puts "Hens", 25 + 30 / 6
4 puts "Roosters", 100 - 25 * 3 % 4
6 puts "Now I will count the eggs:"
8 puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
10 puts "Is it true that 3 + 2 < 5 - 7?"
12 puts 3 + 2 < 5 - 7
14 puts "What is 3 + 2?", 3 + 2
15 puts "What is 5 - 7?", 5 - 7
17 puts "Oh, that's why it's false."
19 puts "How about some more."
21 puts "Is it greater?", 5 > -2
22 puts "Is it greater or equal?", 5 >= -2
23 puts "Is it less or equal?", 5 <= -2
What You Should See
$ ruby ex3.rb
I will now count my chickens:
Now I will count the eggs:
Is it true that 3 + 2 < 5 - 7?
What is 3 + 2?
What is 5 - 7?
Oh, that's why it's false.
How about some more.
Is it greater?
Is it greater or equal?
Is it less or equal?
[yep, that is indeed what I am seeing]
Above each line, use the # to write a comment to yourself explaining what the line does.
Remember in Exercise 0 when you started IRB? Start IRB this way again and using the above characters and what you know, use Ruby as a calculator.
Find something you need to calculate and write a new .rb file that does it.
Notice the math seems "wrong"? There are no fractions, only whole numbers. Find out why by researching what a "floating point" number is.
Rewrite ex3.rb to use floating point numbers so it's more accurate (hint: 20.0 is floating point).
It can be tricky to know what gets worked on first with a string of characters and no designation. For me personally, I like to use parentheses even if the system is smart enough to figure them out (the system might be, but I want to know it's doing what I want it to, so I use the parentheses to keep it straight). Even if we don't use parentheses, the system will use mathematical order of operations to evaluate values, so make sure you are writing them out the way that you intend to... or again, use parentheses if you want to make sure you are calculating the values you think you should be calculating.
I'm getting a couple of errors with <= and <
ex3.rb:23: syntax error, unexpected '=', expecting $end
puts "Is it less or equal?", 5 <= -2
ex3.rb:12:in `': undefined local variable or method `lt' for main:Object (NameError)
I thought my code looked the same as yours:
puts "I will now count my chickens:"
puts "Hens", 25 + 30 / 6
puts "Roosters", 100 - 25 * 3 % 4
puts "Now I will count the eggs:"
puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
puts "Is it true that 3 + 2 < 5 - 7?"
puts 3 + 2 < 5 - 7
puts "What is 3 + 2?", 3 + 2
puts "What is 5 - 7?", 5 - 7
puts "Oh, that's why it's false."
puts "How about some more."
puts "Is it greater?", 5 > -2
puts "Is it greater or equal?", 5 >= -2
puts "Is it less or equal?", 5 <= -2
Hi Cheryl. Based on what you have written here, it looks like it should work as expected. The fact that it doesn't leads me to believe you may have a character that is being interpreted differently.
My suggestion, locate the lines, and delete and retype the statements that correspond with them. Comment them out and see what error messages you get. If you don't, then un-comment them and compare again. This will help you isolate the lines that are causing the issues, and then perhaps by retyping the characters you can overcome any un-intended formatting that might be causing the text to be interpreted differently.
Post a Comment