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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -