Sharpen your Ruby: Part 3
Tags: ruby, rails, beginners, tutorial
Published: 2021-08-25 00:52:33

---
title: Sharpen your Ruby: Part 3
published: true
description: Learn Ruby Programming Language
tags: ruby, rails, beginner, tutorial 
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypw5698bi8gw3d19hl5v.jpeg
series: learn-ruby
canonical_url: https://eric-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: <a href="https://twitter.com/EricTheCoder_" class="twitter-follow-button" data-show-count="false">EricTheCoder_</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> 

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

<h2>Numbers Type in Ruby</h2>
In Ruby the two numbers primary types are Integer and Float. 
```ruby
# Integer
age = 27

# Float
price = 79.99
```

Attention: In Ruby manipulating integers always end up with an integer
```ruby
number = 1 / 100 # 0
```
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
```ruby
number = 1.0 / 100 # 0.01
```
<h2>String to number</h2>
Sometimes you will have a string that represents numbers and would like to perform a math operation on those.
```ruby
number = '1'
other_number = '2'
puts number + other_number # 12
```
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
```ruby
puts number.to_i + other_number.to_i # 3
```
to_i convert string to an integer. You can also use to_f to convert your string to a float number.

<h2>Arithmetic Order of Operations</h2>
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:
```ruby
result = 100**2 + 20 - 200 / (7 - 2) + 150 + 2 * 100
# 10330
```
Here the order of operation to make that result possible
```ruby
# 1. Parentheses
(7-2) = 5
100**2 + 20 - 200 / 5 + 150 + 2 * 100

# 2. Exponent
100**2 = 10000
10000 + 20 - 200 / 5 + 150 + 2 * 100

# 3. Multiplication
2 * 100 = 200
10000 + 20 - 200 / 5 + 150 + 200

# 4. Division
200 / 5 = 40
10000 + 20 - 40 + 150 + 200

# 5. Final Addition and Subtraction
10000 + 20 - 40 + 150 + 200 = 10330

```
<h2>Numbers methods</h2>
Here are some numbers manipulation methods
```ruby
# Round
number.round 2.68  # 3

# Round down
number.floor 2.68  # 2

# Round up
number.ceil 2.68   # 3

# Next
2.next  # 3

# Is number event?
puts 2.even?  # true

# Is number odd?
puts 2.odd?   # false

# Generate a random number
random_number = rand(1..100)
```

<h1>Exercise</h1>
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.

2. Create an arithmetic operation using all the PEMDAS items and try to figure out the result without looking at Ruby result.


Solution
```ruby
number1 = rand(1.0..100).round
# 74

# Example only
number2 = 2**4 + 50 * (10 + 20) / 4
# 391
```

<h3>Conclusion</h3>
That's it for today. The journey just started, stay tuned for the next post very soon

Follow me on Twitter: <a href="https://twitter.com/EricTheCoder_" class="twitter-follow-button" data-show-count="false">EricTheCoder_</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>