Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Sharpen your Ruby: Part 3
Eric The Coder
Eric The Coder

Posted on • Originally published ateric-the-coder.com

     

Sharpen your Ruby: Part 3

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.

Follow me on Twitter:EricTheCoder_

If you have any questions/comments or you are new and need help, you can comment below or send me a message.

Numbers Type in Ruby

In Ruby the two numbers primary types are Integer and Float.

# Integerage=27# Floatprice=79.99
Enter fullscreen modeExit fullscreen mode

Attention: In Ruby manipulating integers always end up with an integer

number=1/100# 0
Enter fullscreen modeExit fullscreen mode

Why does 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 a decimal? Yes just use a float in the equation

number=1.0/100# 0.01
Enter fullscreen modeExit fullscreen mode

String to number

Sometimes you will have a string that represents numbers and would like to perform a math operation on those.

number='1'other_number='2'putsnumber+other_number# 12
Enter fullscreen modeExit fullscreen mode

This operation will return 12. String is not a number.

To make it work we have to convert string to number using the to_i method

putsnumber.to_i+other_number.to_i# 3
Enter fullscreen modeExit fullscreen mode

to_i convert string to an integer. You can also use to_f to convert your string to a float number.

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

An old trick to remember this order is to take the first letter of each item that gives the word PEMDAS

Let's do example:

result=100**2+20-200/(7-2)+150+2*100# 10330
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

Numbers methods

Here are some numbers manipulation methods

# Roundnumber.round2.68# 3# Round downnumber.floor2.68# 2# Round upnumber.ceil2.68# 3# Next2.next# 3# Is number event?puts2.even?# true# Is number odd?puts2.odd?# false# Generate a random numberrandom_number=rand(1..100)
Enter fullscreen modeExit fullscreen mode

Exercise

Create a little program that:

  1. Generate a random number between 1.0 and 100. That will generate a float number.

  2. Use the round method to round your random number.

  3. Create an arithmetic operation using all the PEMDAS items and try to 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
Enter fullscreen modeExit fullscreen mode

Conclusion

That's it for today. The journey just started, stay tuned for the next post very soon

Follow me on Twitter:EricTheCoder_

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Businessman and blogger #Javascript, #Python and #PHP. My favorite frameworks/librairies are #React, #Laravel, and #Django. I am also a fan of #TailwindCSS
  • Location
    Canada
  • Joined

More fromEric The Coder

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp