# Math parser?

**URL:** <https://www.ruby-forum.com/t/math-parser/137946>\
**Category:** Rails\
**Created:** [May 18, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/math-parser/137946 "2008-05-18T20:21:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [May 18, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/math-parser/137946/1 "2008-05-18T20:21:02Z")

</div>

HI guys.

I need to calculate a string as a math piece. E.i. “100\*500/2.5” Is  
there a build in parser in the Math class to do this?

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [May 19, 2008, 1:20am UTC](https://www.ruby-forum.com/t/math-parser/137946/2 "2008-05-19T01:20:26Z")

</div>

> Is there a build in parser in the Math class to do this?

I don’t think so, but we might roll our own.

module Math  
def self.eval(expression)  
allowed\_characters = Regexp.escape('±_/.’)  
safe\_expression =  
expression.match(/[\d#{allowed\_characters}]_/).to\_s  
Kernel.eval(safe\_expression)  
end  
end

I believe the above is safe, but don’t shoot me if it’s not.

Tor Erik

THE SOFTWARE IS PROVIDED â€œAS ISâ€, WITHOUT WARRANTY OF ANY KIND, EXPRESS  
OR IMPLIED, INCLUDING BUT NOT LIMITED …

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [May 19, 2008, 7:17am UTC](https://www.ruby-forum.com/t/math-parser/137946/3 "2008-05-19T07:17:02Z")

</div>

Super. Ill try that. I would need to add this to the math class in the  
ruby root library?

Tor Erik L. wrote:

> > Is there a build in parser in the Math class to do this?
> 
> I don’t think so, but we might roll our own.
> 
> module Math  
> def self.eval(expression)  
> allowed\_characters = Regexp.escape('±_/.’)  
> safe\_expression =  
> expression.match(/[\d#{allowed\_characters}]_/).to\_s  
> Kernel.eval(safe\_expression)  
> end  
> end
> 
> I believe the above is safe, but don’t shoot me if it’s not.
> 
> Tor Erik
> 
> THE SOFTWARE IS PROVIDED â€œAS ISâ€, WITHOUT WARRANTY OF ANY KIND, EXPRESS  
> OR IMPLIED, INCLUDING BUT NOT LIMITED …

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [May 19, 2008, 7:28am UTC](https://www.ruby-forum.com/t/math-parser/137946/4 "2008-05-19T07:28:20Z")

</div>

You can put this file in the lib directory, and call it something simple  
like custom\_math.rb, then in your application.rb do include Math and it  
should include that.

On Mon, May 19, 2008 at 2:47 PM, Emil K. \<  
removed\_email\_address@domain.invalid\> wrote:

> > def self.eval(expression)
> > 
> > THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS  
> > OR IMPLIED, INCLUDING BUT NOT LIMITED …
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Appreciated my help?  
Reccommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [May 19, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/math-parser/137946/5 "2008-05-19T17:44:55Z")

</div>

If you’re willing to wrap it with some error handling you can let  
‘eval’ take care of it:

eval(“100\*500/2.5”)  
=\> 20000.0

(It understands parens, too).

> > eval(“100\*(500/2.5)”)  
> > =\> 20000.0

On May 19, 1:27 am, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [May 19, 2008, 9:38pm UTC](https://www.ruby-forum.com/t/math-parser/137946/6 "2008-05-19T21:38:51Z")

</div>

AndyV wrote:

> If you’re willing to wrap it with some error handling you can let  
> ‘eval’ take care of it

Just be carefull so you don’t unintentionally allow arbitrary code to be  
executed. (Depending on the use case this may or may not be an issue).

If you change  
Regexp.escape(‘±\*/.’)

to

Regexp.escape('±\*/.() ')

then you can use parentheses and spaces in my example too.

Tor Erik

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [November 24, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/math-parser/137946/7 "2008-11-24T20:17:34Z")

</div>

> HI guys.
> 
> I need to calculate a string as a math piece. E.i. “100\*500/2.5” Is  
> there a build in parser in the Math class to do this?

Hi, I have just written a few blog posts about building a math parser.  
There is an introduction, some basic knowledge about grammars, sample  
implementation written in Ruby and a test suite. You can find these  
posts here:

- [Math Parser, Part 1: Introduction - Lukasz Wrobel](http://lukaszwrobel.pl/blog/math-parser-part-1-introduction)
- [Math Parser, Part 2: Grammar - Lukasz Wrobel](http://lukaszwrobel.pl/blog/math-parser-part-2-grammar)
- [Math Parser, Part 3: Implementation - Lukasz Wrobel](http://lukaszwrobel.pl/blog/math-parser-part-3-implementation)
- [Math Parser, Part 4: Tests - Lukasz Wrobel](http://lukaszwrobel.pl/blog/math-parser-part-4-tests)

If you find the code useful, feel free to use it in your own program.
