Skip to content

Instantly share code, notes, and snippets.

@volpeo
Created January 14, 2019 10:33
Show Gist options
  • Save volpeo/14581896207accf52775dc13012537df to your computer and use it in GitHub Desktop.
Save volpeo/14581896207accf52775dc13012537df to your computer and use it in GitHub Desktop.
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
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