Search code examples
rubyparsingmethodsstring-parsing

convert string to method


I have a string that can be '+', '-', '*' or '/', and two numbers. I need to apply the operation denoted by the string to the numbers. I tried:

op = '+'
(&op.to_sym).call 1, 2

but it won't parse it. please help.


Solution

  • Ruby is not a Polish notation language.

    1.send(op, 2)