Project

arithmetic

0.0
No commit activity in last 3 years
No release in over 3 years
Simple arithmetic calculator for Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
>= 0
 Project Readme

arithmetic

Simple arithmetic evaluator for Ruby

Based on http://rosettacode.org/wiki/Arithmetic_evaluation#Ruby but adds:

  • tests
  • support for nicer errors on invalid expressions
  • optional spaces between operators and operands
  • uses BigDecimal instead of floats

Shortcomings:

  • only supports + - * / operators
  • no localization

Usage

expression = Arithmetic.parse("-2 * (1+3.5)")
expression.eval # => -18
expression.to_s # => "-2 * (1 + 3.5)"

Arithmetic.parse("2 + wtf?") # => raises Arithmetic::InvalidExpression with the 
                             # original expression as the message

I18N support

Please ensure that decimal separators are decimals ('.'). Use a gem like Delocalize to perform this conversion.