Postfiks is an implementation of the reverse Polish notation (using stacks) in the form of a parser which tokenizes the expression and then evaluates it.
require 'postfiks'
'2 2 +'.postfix
# => #<BigDecimal:7fad2e391a38,'0.4E1',9(18)>
'2 2 +'.postfix == 4
# => true
Install the latest stable version of Postfiks via RubyGems:
$ gem install postfiks
Postfiks provides a helper method for the String
class, called #postfix
. This method can be called on any String — Postfiks will evaluate the expression within that String and the resulting numerical value will be of type BigDecimal
.
'30 2 * 3 3 * +'.postfix
# => #<BigDecimal:7fad2e301690,'0.69E2',9(18)>
'30 2 * 3 3 * +'.postfix == 69
# => true
'2 2 + 5.51 * 2 - 12 *'.postfix
# => #<BigDecimal:7fad2ca99af8,'0.24048E3',18(36)>
'2 2 + 5.51 * 2 - 12 *'.postfix == 240.48
# => true