Ruby evaluate without eval?

后端 未结 5 470
野性不改
野性不改 2021-01-19 14:32

How could I evaluate at mathematical string without using eval?

Example:

mathstring = \"3+3\"

Anyway that can be evaluated without

相关标签:
5条回答
  • 2021-01-19 15:04

    You must either or eval it, or parse it; and since you don't want to eval:

    mathstring = '3+3'
    i, op, j = mathstring.scan(/(\d+)([+\-*\/])(\d+)/)[0] #=> ["3", "+", "3"]
    i.to_i.send op, j.to_i #=> 6
    

    If you want to implement more complex stuff you could use RubyParser (as @LBg wrote here - you could look at other answers too)

    0 讨论(0)
  • 2021-01-19 15:06

    I'm assuming you don't want to use eval because of security reasons, and it is indeed very hard to properly sanitize input for eval, but for simple mathematical expressions perhaps you could just check that it only includes mathematical operators and numbers?

    mathstring = "3+3"
    puts mathstring[/\A[\d+\-*\/=. ]+\z/] ? eval(mathstring) : "Invalid expression"
    => 6
    
    0 讨论(0)
  • 2021-01-19 15:08

    You have 3 options:

    1. In my honest opinion best - parse it to Reverse Polish Notation and then parse it as equation
    2. As you say use RegExps
    3. Fastest, but dangerous and by calling eval but not Kernel#eval

      RubyVM::InstructionSequence.new(mathstring).eval
      
    0 讨论(0)
  • 2021-01-19 15:08

    Dentaku seems (I haven't used it yet) like a good solution - it lets you check your (mathematical and logical) expressions, and to evaluate them.

    calculator = Dentaku::Calculator.new
    calculator.evaluate('kiwi + 5', kiwi: 2)
    
    0 讨论(0)
  • 2021-01-19 15:12

    Sure--you'd just want to somehow parse the expression using something other than the bare Ruby interpreter.

    There appear to be some good options here: https://www.ruby-toolbox.com/search?q=math

    Alternatively, it probably wouldn't be that hard to write your own parser. (Not that I've seriously tried--I could be totally full of crap.)

    0 讨论(0)
提交回复
热议问题