ruby - Performing mathematical operations on numbers within a string -
given string containing comma-separated numbers, possible perform mathematical operations on them?
for example, how can take string "123,456,789"
, extract numbers within it, , perform operations 123 + 456
or 456 - 123
?
since you're messing around, can use eval
, gsub
>> eval '123,456,789'.gsub(',', '*') # 44253432 >> eval '123,456,789'.gsub(',', '+') # 1368 >> eval '123,456,789'.gsub(',', '-') # -1122
Comments
Post a Comment