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 ...
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.