Skip to content

Instantly share code, notes, and snippets.

@Sixeight
Created September 21, 2008 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sixeight/11839 to your computer and use it in GitHub Desktop.
Save Sixeight/11839 to your computer and use it in GitHub Desktop.
require 'stack'
class RPN
def initialize
@stack = Stack.new
end
def calc(expr)
expr.split.each do |e|
if %w[ + - * / ].include? e
i = @stack.pop
@stack.push @stack.pop.send(e, i)
else
@stack.push e.to_i
end
end
@stack.pop
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment