Reputation: 2240
How would you convert a string like "2 + 3 + 4" into an expression that ruby will return the value of 9
but as an integer. I was thinking some sort of .each block, but I feel that would be a very long function. If that is the only way, then fine. Just wanted some other eyes on it.
Upvotes: 2
Views: 1796
Reputation: 10074
Take a look at Kernel#eval
[~]$ irb
2.1.2 :001 > eval("2 + 3 + 4")
=> 9
2.1.2 :002 > eval("2 + 3 + 4").class
=> Fixnum
Hope this helps
Upvotes: 2