class String def rpn @expr = self.split(" ") stack = Array.new @expr.each {|i| if i == "+" || i == "-" || i == "*" || i == "/" stack = calc(i, stack) else stack < < i end } return stack[0] end private def calc(operator, stack) return Array(stack[1..-1].inject(stack[0].to_i){|result, i| reslut = result.__send__(operator, i.to_i) }) end end require 'rpn.rb' "2 8 +".rpn #=> 10 "4 6 -".rpn #=> -2 "81