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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With