DEV Community

Cover image for Simple Ruby Methods
rwparrish
rwparrish

Posted on • Updated on

Simple Ruby Methods

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

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}"
Enter fullscreen mode Exit fullscreen mode

In the code snippet above to_f is used to convert the user input from a string to a Float. 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
Enter fullscreen mode Exit fullscreen mode

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)}"
Enter fullscreen mode Exit fullscreen mode

Above you can see how we call a method while using interpolation. 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.

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!

Top comments (0)