Created
April 9, 2018 10:45
-
-
Save volpeo/93af8bab71a448d1f964ec7f6706184a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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