Skip to content

Instantly share code, notes, and snippets.

@kirikiriyamama
Created September 14, 2013 14:28
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 kirikiriyamama/6562489 to your computer and use it in GitHub Desktop.
Save kirikiriyamama/6562489 to your computer and use it in GitHub Desktop.
injectで逆ポーランド記法
def rpn(expression, separator = '')
expression.split(separator).inject([]) do |stack, token|
if token =~ /[\d.]+/
stack.push token.to_f
else
stack.push stack.pop(2).inject(token)
end
end.pop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment