An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
Sharpen your Ruby: Working with Numbers
I develop in Javascript, Python, PHP and Ruby. By far Ruby is my favorite programming language.
Together let start a journey and revisit our Ruby foundations.
Each post will include some theory but also exercise and solution.
If you have any questions/comments or your are new and need help, you can comment below or send me a message.
Numbers Type in Ruby
In Ruby the two numbers primary type are Integer and Float.
# Integerage = 27# Floatprice = 79.99
Attention: In Ruby manipulating integers always end up with an integer
number = 1 / 100 # 0
Why this division return 0? Because both numbers are integer and Ruby assumed that you would want a rounded value, so it rounded it to the nearest whole number, which is 0 in this case.
Ok but what can I get the right value with decimal? Yes just use a float in the equation
number = 1.0 / 100 # 0.01
Arithmetic Order of Operations
If we try to run this code what will be the value? In which order the operation will be run?
In Ruby the order of operation is
- Parentheses
- Exponent
- Multiplication
- Division
- Addition
- Subtraction
A old trick to remember this order is to take the first letter of each item that give the word PEMDAS
Let's do example:
result = 100**2 + 20 - 200 / (7 - 2) + 150 + 2 * 100# 10330
Here the order of operation to make that result possible
# 1. Parentheses(7-2) = 5100**2 + 20 - 200 / 5 + 150 + 2 * 100# 2. Exponent100**2 = 1000010000 + 20 - 200 / 5 + 150 + 2 * 100# 3. Multiplication2 * 100 = 20010000 + 20 - 200 / 5 + 150 + 200# 4. Division200 / 5 = 4010000 + 20 - 40 + 150 + 200# 5. Final Addition and Subtraction10000 + 20 - 40 + 150 + 200 = 10330
Numbers methods
Here are some numbers manipulation methods
# Roundnumber.round 2.68 # 3# Round downnumber.floor 2.68 # 2# Round upnumber.ceil 2.68 # 3# Next2.next # 3# Is number event ?puts 2.even? # true# Is number odd ?puts 2.odd? # false# Generate a random numberrandom_number = rand(1..100)
Exercice
Create a little program that:
Generate a random number between 1.0 and 100. That will generate a float number.
Use the round method to round your random number.
Create an arithmetic operation using all the PEMDAS item and try figure out the result without looking at Ruby result.
Solution
number1 = rand(1.0..100).round# 74# Example onlynumber2 = 2**4 + 50 * (10 + 20) / 4# 391
Conclusion
That's it for today. The journey just started, stay tune for the next post very soon. (later today or tomorrow)
If you have any comments or questions please do so here or send me a message on twitter.
I am new on twitter so if you want to make me happy
Follow me: Follow @justericchapman
Original Link: https://dev.to/ericchapman/sharpen-your-ruby-working-with-numbers-3ki9
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To