Pages

Wednesday, November 2, 2011

Exercise 3: Numbers And Math: Learn Ruby The Hard Way: Practicum


Performing calculations is an important part of any programming language, and Ruby is no exception. Many people have been led to believe that to be a computer expert you have to be a mathematics expert (and Computer Science and Engineering requirements certainly don't dispel that rumor; it's a lot of the reason why I have an I.S. degree rather than a C.S. or E.E. degree ;) ). Still in many instances, the math that is performed is actually pretty straightforward. Most of the math symbols will be perfectly familiar to anyone who has done any kind of programming in the past or even worked with Excel.

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:"
 2
 3 puts "Hens", 25 + 30 / 6
 4 puts "Roosters", 100 - 25 * 3 % 4
 5
 6 puts "Now I will count the eggs:"
 7
 8 puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
 9
10 puts "Is it true that 3 + 2 < 5 - 7?"
11
12 puts 3 + 2 < 5 - 7
13
14 puts "What is 3 + 2?", 3 + 2
15 puts "What is 5 - 7?", 5 - 7
16
17 puts "Oh, that's why it's false."
18
19 puts "How about some more."
20
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

[see below]


What You Should See

$ ruby ex3.rb
I will now count my chickens:
Hens
30
Roosters
97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
false
What is 3 + 2?
5
What is 5 - 7?
-2
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
$

[yep, that is indeed what I am seeing]

Extra Credit

Above each line, use the # to write a comment to yourself explaining what the line does.
[see below]



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.

[see below]


Find something you need to calculate and write a new .rb file that does it.
[see below]



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).

[see below]



TESTHEAD's TAKEAWAYS

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.

2 comments:

  1. 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

    Any advice?

    ReplyDelete
  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.

    ReplyDelete