可能重复:
How to turn a string into a method call?使用方法名的字符串/变量调用一个方法inRuby
目前,我有一个代码,做这样的事情
def execute
case @command
when "sing"
sing()
when "ping"
user_defined_ping()
when "--help|-h|help"
get_usage()
end
我发现这个案子非常无用,而且我非常喜欢通过使用va来调用适当的方法riable @命令。喜欢的东西:
def execute
@command()
end
Offcourse我不需要在这种情况下额外的execute()方法。
有关我如何实现这个红宝石的任何建议?
谢谢!
编辑: 为多个字符串添加了其他方法类型。不知道这是否也可以以优雅的方式处理。
重复的http://stackoverflow.com/questions/4800836/call-a-method-on-a-variable-where-the-method-name-is-in-another-variable和http:// stackoverflow.com/questions/6317298/how-to-turn-a-string-into-a-method-call和其他许多 – millimoose