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

def addition(val1, val2)
    return val1+val2
end

add = addition(val1, val2)

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 == "+"
puts add
elsif sym == "-"
puts sub
elsif sym == "/"
puts div
elsif sym == "*"
puts mult
else puts "error"
end
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.

For example instead of 

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


Why not 

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