I am trying to do something like this. It is giving me an error, which I guess is because op
is a string. Is it possible to convert a string of a math operator to be an operator?
def calc(op)
a = 9
b = 5
a op b
end
p calc('-')
p calc('+')
I am trying to do something like this. It is giving me an error, which I guess is because op
is a string. Is it possible to convert a string of a math operator to be an operator?
def calc(op)
a = 9
b = 5
a op b
end
p calc('-')
p calc('+')
Here it is using Object#send
:
def calc(op)
a = 9
b = 5
a.send(op,b)
end
p calc('-')
p calc('+')
# >> 4
# >> 14