Skip to content

Instantly share code, notes, and snippets.

@MrBean83
Created August 13, 2013 20:51
Show Gist options
  • Save MrBean83/6225555 to your computer and use it in GitHub Desktop.
Save MrBean83/6225555 to your computer and use it in GitHub Desktop.
"RPN Calculator"
class RPNCalculator
def evaluate(rpn)
rpn = rpn.split
array = rpn.inject([]) do |array, i|
if i.match(/\d+/)
array = i.to_i
else
eq = array.pop(2)
case
when i == "+"
array << eq[0] + eq[1]
when i == "-"
array << eq[0] - eq[1]
when i == "*"
array << eq[0] * eq[1]
end
end
end
puts array.pop
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment