# Simple calculator in Ruby

I wrote a calculator in Ruby. These are the rules:

• Write the first number, in a new line the arithmetic operator, and in another line the last number.

• Use + for addition, - for subtraction, * for multiplication and / for division.

Implementation:

val1 = gets.to_i
sym = gets.chomp
val2 = gets.to_i

return val1+val2
end

def subtraction(val1, val2)
return val1-val2
end

sub = subtraction(val1, val2)

def division(val1, val2)
return val1/val2
end

div = division(val1, val2)

def multiplication(val1, val2)
return val1*val2
end

mult = multiplication(val1, val2)

if sym == "+"
elsif sym == "-"
puts sub
elsif sym == "/"
puts div
elsif sym == "*"
puts mult
else puts "error"
end

• Welcome to Code Review. For the future, the title of a question should be a brief summary of the code. I changed it for you this time, feel free to further improve it if you want. To make the most out of your post, please read How to Ask. Commented Aug 26, 2017 at 19:22
• Do you have anything particular you think needs improvement? What aspects are you looking for help with? Commented Aug 26, 2017 at 22:36
• Well I know it works, but I would like to know if my code is functional, I wonder if there isn't an easier or better way of doing the same thing. And also I don't know how I could make it work on more than two numbers. Commented Aug 27, 2017 at 3:40

Instead of calculating all the function returns and then checking which one the user input why not just run the function after checking the users input.

add = addition(val1, val2)
if sym == "+"


Why not

if sym == "+"

You might put the four functions into a map, with +-*/ characters as the keys, and dispatch in that way, rather than using if statements.