Created
January 14, 2019 10:33
-
-
Save volpeo/14581896207accf52775dc13012537df 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
POSSIBLE_OPERATORS = %w(* / + - %) | |
def process_calculation(first_number, second_number, operator) | |
return nil unless POSSIBLE_OPERATORS.include? operator | |
# case operator | |
# when "+" | |
# first_number.+(second_number) | |
# when "-" | |
# first_number.-(second_number) | |
# when "*" | |
# first_number.*(second_number) | |
# when "/" | |
# first_number./(second_number) | |
# end | |
first_number.send(operator, second_number) | |
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" | |
continue = "y" | |
while continue.start_with? "y" | |
print "operand> " | |
first_number = gets.chomp.to_i | |
operator = nil | |
until POSSIBLE_OPERATORS.include? operator | |
print "operator (#{POSSIBLE_OPERATORS.join(', ')})> " | |
operator = gets.chomp | |
end | |
print "operand> " | |
second_number = gets.chomp.to_i | |
result = process_calculation(first_number, second_number, operator) | |
puts "Result: #{result}" | |
print "continue? (Y/N)> " | |
continue = gets.chomp.downcase | |
end | |
puts "Bye bye" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment