Skip to content

Instantly share code, notes, and snippets.

@tlands
Last active December 18, 2015 12:49
Show Gist options
  • Save tlands/5785744 to your computer and use it in GitHub Desktop.
Save tlands/5785744 to your computer and use it in GitHub Desktop.
Here's a Ruby version of RPN calculator.
class RPNCalculator
def evaluate(str)
ary = []
str.split.each do |i|
if i.match(/\d/)
ary << i.to_i
elsif i.match(/\-|\+|\*/)
operator = i.to_sym
ary << ary.pop(2).inject(operator)
end
end
return ary.join.to_i
end
end
calc = RPNCalculator.new
calc.evaluate('70 10 4 + 5 * -') # => 0
@tlands
Copy link
Author

tlands commented Jun 14, 2013

WHY DOES iT iNDENT iT SO AWFULLY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment