문제

I want to either add or subtract

so I pass in 'action' as a paramater

How do I do a 'action' b for a+b or a-b

I tried eval but no luck, undefined method 'action'

도움이 되었습니까?

해결책

Use send.

action = "+"
a.send(action, b)

다른 팁

Eval works for me.

2.0.0p247 :006 > action = '+'
 => "+"
2.0.0p247 :007 > eval("5 #{action} 2")
 => 7
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top