Simple Ruby Methods
Tags: beginners, ruby, codenewbie
Published: 2020-11-01 02:47:54

Creating a simple calculator using Ruby is fun. It's also a great way to learn how to use methods in Ruby. That is what this blog will be about.

If you would like to code along, just open a new tab and go to repl.it. Once there simply click on "start coding" and select Ruby as the language - that's it. Practicing coding is always the best way to learn so I encourage you to try it.


First, let's get some basic code down to work with. Then, we will look at how to move the logic to methods that can be called on throughout the app.

Enter the code below and click "Run":
```
puts "Simple calculator"
25.times { print "-" }
puts
puts "Enter the first number"
num_1 = gets.chomp
puts "Enter the second number"
num_2 = gets.chomp
```
In Ruby "puts" is used to print something to the screen for the user to see. The line of code `25.times { print "-" }` is what creates the line under the text "Simple calculator". Try changing the `25.times { print "-" }` to `25.times { puts "-" }` and notice the difference. Now change it back because we want the line effect. Line three in the code acts as a spacer bumping `puts "Enter the first number"` down a line. Next, the user is prompted to enter a number. To capture the user input we use `gets.chomp` and assign it to the variable `num_1` - we need a place to store the user input so we can use it later. The last two lines do the same thing.

Now for the logic:
```
puts "the first number multiplied by the second number is: #{num_1.to_f * num_2.to_f}"
puts "the first number divided by the second number is: #{num_1.to_f / num_2.to_f}"
```
In the code snippet above `to_f` is used to convert the user input from a string to a [Float](https://docs.ruby-lang.org/en/2.4.0/Float.html). Without this, Ruby would throw an error because we would be attempting to multiply two strings together. The user input is a string that needs to be converted to a number so the math can be performed.

Now let's build a method to handle our logic:
```
def multiply(first_num, second_num)
  first_num.to_f * second_num.to_f
end

def divide(first_num, second_num)
  first_num.to_f / second_num.to_f
end
```
The methods should take two arguments - our two numbers from the user. `def` to define the method name (multiple and divide are the names above), the two arguments in the parenthesis, the logic to be performed on/with the arguments, and `end` to end the method.

Now we call the method(s). It's important to note that the methods need to be above their call in the code because Ruby runs line by line.

Check it out:
```
puts "the first number multiplied by the second number is: #{multiply(num_1, num_2)}"
puts "the first number divided by the second number is: #{divide(num_1, num_2)}"
puts "the first number added the second number is: #{addition(num_1, num_2)}"
puts "the first number minus the second number is: #{subtract(num_1, num_2)}"
puts "the first number mod the second number is: #{mod(num_1, num_2)}"
```
Above you can see how we call a method while using [interpolation](http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html). We call the method by name and provide it the two variables we stored our user input in - `#{multiply(num_1, num_2)}`. See if you can write methods for addition, subtraction, and [modulo](https://www.rubyguides.com/2019/01/ruby-math/).

##Recap##

In this read, we covered some basic logic in Ruby. We learned how to hard code the logic and how to export the logic to a method that we can then call using interpolation.

I hope you learned something and had fun. Ask a question and/or leave some feedback! Happy coding!