Skip to content

Instantly share code, notes, and snippets.

@volpeo
Created April 9, 2018 10:45
Show Gist options
  • Save volpeo/93af8bab71a448d1f964ec7f6706184a to your computer and use it in GitHub Desktop.
Save volpeo/93af8bab71a448d1f964ec7f6706184a to your computer and use it in GitHub Desktop.
# def compute(a, b, operator)
# case operator
# when "+" then a + b
# when "-" then a - b
# when "*" then a * b
# when "/" then a / b
# when "%" then a % b
# else "Not implemented"
# end
# end
def compute(a, b, operator)
return "Not implemented" unless POSSIBLE_OPERATORS.include? operator
a.send(operator, b)
end
require_relative "calculator"
def ask_user_for_number
print "number> "
gets.chomp.to_i
end
POSSIBLE_OPERATORS = ["+", "-", "/", "*", "%"]
user_answer = "Y"
until user_answer.downcase.start_with? "n"
first_number = ask_user_for_number
second_number = ask_user_for_number
print "operator #{POSSIBLE_OPERATORS.join(', ')}> "
operator = gets.chomp
print "#{first_number} #{operator} #{second_number} = "
puts compute(first_number, second_number, operator)
puts "Do another calculation?"
print "Y/N> "
user_answer = gets.chomp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment