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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 ... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Super. Ill try that. I would need to add this to the math class in the ruby root library? Tor Erik Linnerud 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 ...-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Kampp < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Super. Ill try that. I would need to add this to the math class in the > ruby root library? > > Tor Erik Linnerud 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 ... > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Appreciated my help? Reccommend me on Working With Rails http://workingwithrails.com/person/11030-ryan-bigg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Bigg (Radar)" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Kampp < > > > > rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > Super. Ill try that. I would need to add this to the math class in the > > ruby root library? > > > Tor Erik Linnerud 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 ... > > > -- > > Posted viahttp://www.ruby-forum.com/. > > -- > Appreciated my help? > Reccommend me on Working With Railshttp://workingwithrails.com/person/11030-ryan-bigg--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
AndyV wrote:> If you''re willing to wrap it with some error handling you can let > ''eval'' take care of itJust 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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: * http://lukaszwrobel.pl/blog/math-parser-part-1-introduction * http://lukaszwrobel.pl/blog/math-parser-part-2-grammar * http://lukaszwrobel.pl/blog/math-parser-part-3-implementation * http://lukaszwrobel.pl/blog/math-parser-part-4-tests If you find the code useful, feel free to use it in your own program. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---