Skip to content

Instantly share code, notes, and snippets.

@alexanderlarson
Created August 17, 2013 23:54
Show Gist options
  • Save alexanderlarson/6259233 to your computer and use it in GitHub Desktop.
Save alexanderlarson/6259233 to your computer and use it in GitHub Desktop.
Working on an RPN calculator
def rpn(expression)
operands = []
while expression.length > 0
token = expression.shift # puts first value of expression into variable "token"
unless token =~ /^\d+$/ # Finding operators
right = operands.pop
left = operands.pop
token = eval "#{left} #{token} #{right}" #eval is really handy!
end
operands << token # push the evaluated value into the operands array
end
operands.pop # returns last value from operands array
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment