Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Created August 13, 2013 05:20
Show Gist options
  • Save TGOlson/6218116 to your computer and use it in GitHub Desktop.
Save TGOlson/6218116 to your computer and use it in GitHub Desktop.
Reverse Polish Calculator - Version 1 (much better!)
# version 2 of 2
# updated to handle negative numbers, removed while loop, and uses send method
class RPNCalculator
def evaluate(string)
array = string.split
output = []
array.each do |x|
if not x =~ /-?\d/ #if current item is not a digit
holder = output.pop(2)
x = holder[0].send(x, holder[1])
end
output << x.to_i
end
output.pop
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment