Ruby evaluate without eval?

后端 未结 5 480
野性不改
野性不改 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: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.)

提交回复
热议问题