Skip to content

Instantly share code, notes, and snippets.

@d0ppler
Created April 9, 2015 10:54
Show Gist options
  • Save d0ppler/f7ab7df326aae21e4f21 to your computer and use it in GitHub Desktop.
Save d0ppler/f7ab7df326aae21e4f21 to your computer and use it in GitHub Desktop.
def calculator
puts "\tGreetings humanoid, im the Calculator.\n\tEnter 'q' to quit.\n\n\tsyntax: [value] [operator] [value]\n"
while true
prompt = '>> '
print prompt
str = gets.chomp.split(" ") # splits input into array
return if str[0] == 'q'
operand1 = str[0].to_i
operand2 = str[2].to_i
operator = str[1].to_sym
case operator
when :+ then puts operand1 + operand2
when :- then puts operand1 - operand2
when :* then puts operand1 * operand2
when :/ then puts operand1 / operand2
when :% then puts operand1 % operand2
else
puts "invalid input"
end
end
end
if __FILE__ == $0
calcudator
end
puts "Okay :( , bye!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment