Rudiments of Ruby : Level Zero.One
Exercise 3: Numbers and Math( The subject I love the most
)
This chapter is about math symbols, I loved the way the author encouraged the math-haters to try this exercise.
“Do not worry, programmers lie frequently about being math geniuses when they really aren’t. If they were math geniuses, they would be doing math, not writing ads and social network games to steal people’s money”
Now to some serious business, this exercise starts with familiarizing most commonly used math symbols. Figure what operation is performed by those symbol, for the below code.
No surprises, there were some typos so resulted in error.
It was expecting a comma instead of a dot, that’s the difference I can make out from other statements, let see if replacing the dot with comma executes the script without any error. Previous error was not shown now, but there was some other error this time.
Undefined method ‘Puts’, looking at the code I was able to spot the problem quickly as the puts with capital P colour differs. How did I miss at first place? The reason is I was more focusing on the maths part, my brain already got familiar with puts and assumes may not make mistakes with puts.
Important learning : Ruby statements are case-sensitive
Now correcting those statements, the execution was successful.
Extra Credits
1. Above each line, use the # to write a comment yourself what the line does.
This exercise looked interesting as it helped me to analyse the way precedence of each symbol. Also commenting helped me to observe the results
puts “Roosters”, 100-25*3%4 prints
Roosters
97
Not
Roosters, 97
or
Roosters 97
2. 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.
3. Find something you need to calculate and write a new .rb file that does it.
Did some interesting calculations interactively with Ruby, check out the below image
4. Notice the math seems “wrong” There no fractions, only whole numbers. Find out why by researching what a “floating point” number is.
Yes, it was clear while trying to get this 3+2+1-5+4%2-1/4+6 result to 7. After few permutation and combinations figured out that the result would be 7 only if 1/4 = zero and not 0.25. Floating point number is a way to represent the numbers decimal precision, this can float mean placing anywhere on the significant digits of the number
5. Rewrite ex3.rb to use floating point numbers so it’s more accurate (hint: 20.0 is floating point)
The hint looks more of reveling the result, so replacing integer with a floating numbers like 22.0/7 or 22/7.0 or 22.0/7.0 should be the solution, so let me check this with extra credit 3. Here is the result
And here is the output of the first exercise
So, this (3+2+1-5)+(4%2)-(1/4.0)+6 = (1)+(0) -(0.25)+6 = 6.75. So which confirms the precedence is correct.
–End of Zero.One –
Oops!!! This exercise started asking the readers to figure out operations performed by those symbol, for the below code.
+ Plus —–> Addition
- minus —–> subtraction
/ slash —–> Division
* asterisk —–> Multiplication
% percent —–> Modulus, returns whole number, the remainder after dividing
< less-than —–> checks if LHS is less than RHS, if yes returns TRUE else FALSE
> greater-than —–> checks if LHS is greater than RHS, if yes returns TRUE else FALSE
<= less-than-equal —–> checks if LHS is less than or equal to RHS, if yes returns TRUE else FALSE
>= greater-than-equal —–> checks if LHS is greater than or equal to RHS, if yes returns TRUE else FALSE
Happy Learning!!!!











