当名称包含在字符串变量中时,如何动态调用方法?例如:
class MyClass
def foo; end
def bar; end
end
obj = MyClass.new
str = get_data_from_user # e.g. `gets`, `params`, DB access, etc.
str #=> "foo"
# somehow call `foo` on `obj` using the value in `str`.
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?这样做是否存在安全风险?