Forum: Ruby on Rails ruby mathematical expression parser

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ff7c968ec96d769974df09dc453eb746?d=identicon&s=25 Sean Blezard (Guest)
on 2006-06-06 15:28
(Received via mailing list)
Hi,

Has anyone come across a Ruby mathematical expression parser, capable
of parsing out token, replacing them with values then executing the
mathematical formula? Nothing too heavy, A * (44/12) * ... etc. where
A is can be replaced with a value.

In Java there are serveral, like JEP, but I'd prefer it we could get a
Ruby one now we have moved over to Rails, otherwise we will have to
have some sort of Java calculation web service which would be quite
faffy.

Thanks

Sean
C70264c74698cbe2324b91ff778abcff?d=identicon&s=25 Daniel Völkerts (Guest)
on 2006-06-07 11:31
(Received via mailing list)
Sean Blezard schrieb:
> Hi,
>
> Has anyone come across a Ruby mathematical expression parser, capable
> of parsing out token, replacing them with values then executing the
> mathematical formula? Nothing too heavy, A * (44/12) * ... etc. where
> A is can be replaced with a value.

Mmh, I've looking similar. Whats about

formular.gsub!(/A/,"0.2")

result = eval(formular)

?

g,
7a28bf7b0932a9486c21f1a33eb431ba?d=identicon&s=25 Tom Lieber (Guest)
on 2006-06-07 14:33
(Received via mailing list)
On 6/6/06, Sean Blezard <sean.blezard@gmail.com> wrote:
> Has anyone come across a Ruby mathematical expression parser, capable
> of parsing out token, replacing them with values then executing the
> mathematical formula? Nothing too heavy, A * (44/12) * ... etc. where
> A is can be replaced with a value.
>
> In Java there are serveral, like JEP, but I'd prefer it we could get a
> Ruby one now we have moved over to Rails, otherwise we will have to
> have some sort of Java calculation web service which would be quite
> faffy.

I don't know of one exactly, but what if you stripped out everything
but 0-9, ., *, /, (, and ), then passed it to a Ruby eval method? Are
there any safety concerns I'm neglecting?

> Thanks
>
> Sean

Sincerely,

Tom Lieber
http://AllTom.com/
http://GadgetLife.org/
32c8942dcd4944eb5f52749d6bbd8b6c?d=identicon&s=25 Brad Long (xagyg)
on 2009-09-02 02:00
Sean Blezard wrote:
> Hi,
>
> Has anyone come across a Ruby mathematical expression parser, capable
> of parsing out token, replacing them with values then executing the
> mathematical formula? Nothing too heavy, A * (44/12) * ... etc. where
> A is can be replaced with a value.
>
[stuff deleted]

Try this...

http://www.chipstips.com/?p=119
D3bcb48eb7c81b50b4eac82f009001dd?d=identicon&s=25 Markus Roberts (Guest)
on 2009-09-02 02:28
(Received via mailing list)
> Sean Blezard wrote:
>> Hi,
>>
>> Has anyone come across a Ruby mathematical expression parser, capable
>> of parsing out token, replacing them with values then executing the
>> mathematical formula? Nothing too heavy, A * (44/12) * ... etc. where
>> A is can be replaced with a value.

Here's how I do it:

http://github.com/MarkusQ/ruby-symbolic-expression...

-- MarkusQ
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-09-03 01:03
Sean Blezard wrote:
> Hi,
>
> Has anyone come across a Ruby mathematical expression parser, capable
> of parsing out token, replacing them with values then executing the
> mathematical formula? Nothing too heavy, A * (44/12) * ... etc. where
> A is can be replaced with a value.
>
> In Java there are serveral, like JEP, but I'd prefer it we could get a
> Ruby one now we have moved over to Rails, otherwise we will have to
> have some sort of Java calculation web service which would be quite
> faffy.
>
> Thanks
>
> Sean

Ridiculous.  You won't need a calculation Web service.  Even if nothing
exists off the shelf in Ruby, it would be very easy to generate a parser
with Treetop.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
This topic is locked and can not be replied to.