开发者

convert string calculation to value of calculation rails

I have a string that looks like this:

"(12.0+10)*31"

I need to be able to convert that to the value:

(12.0+10)*31 = 682

I have tried some obvious ones like .to_f but that didn't work

I only care about the valu开发者_如何学Pythone (682), I don't really need to see the calculation.

I am sure there is some easy rails thing that does this that I can't find.

Thanks for any help


You can use eval for this, i.e. in IRB:

>> eval "(12.0+10)*31"
=> 682.0

Then if it needs to be an integer you just call to_i on the result.

ian.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜