Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created January 28, 2019 21:37
Show Gist options
  • Save caioertai/9058c27a714d83f109730b5a12816ab4 to your computer and use it in GitHub Desktop.
Save caioertai/9058c27a714d83f109730b5a12816ab4 to your computer and use it in GitHub Desktop.
def calculate(first_number, operator, second_number)
case operator
when '+' then first_number + second_number
when '-' then first_number - second_number
when '*' then first_number * second_number
when '/' then first_number / second_number.to_f
else "Invalid operator"
end
end
require_relative 'calculator'
puts "Welcome to the calculator"
loop_input = 'y'
until loop_input == 'n'
puts "What's the first number you want to operate on?"
first_number = gets.chomp.to_i
puts "Which operator? (+|/|*|-)"
operator = gets.chomp
puts "What's the second number you want to operate on?"
second_number = gets.chomp.to_i
puts "The result is:"
result = calculate(first_number, operator, second_number)
puts result
puts "Do you want to calculate again? (y/n)"
loop_input = gets.chomp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment