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 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?
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 …
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
endI 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 …
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. <
[email protected]> 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/.
–
Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg
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)” [email protected]
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
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:
If you find the code useful, feel free to use it in your own program.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs