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
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 > > SeanSincerely, Tom Lieber http://AllTom.com/ http://GadgetLife.org/
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, -- Daniel V?lkerts Protected by Anti Pesto. -- Wallace & Gromit
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 -- Posted via http://www.ruby-forum.com/.
> 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-expressions/tree/master -- MarkusQ
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 > > SeanRidiculous. 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-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.