<分区>
Possible Duplicate:
How to turn a string into a method call?
目前我有一个代码正在做这样的事情
def execute
case @command
when "sing"
sing()
when "ping"
user_defined_ping()
when "--help|-h|help"
get_usage()
end
我发现这个案例非常无用且庞大,我想通过使用变量@command 来调用适当的方法。像这样的东西:
def execute
@command()
end
Offcourse 在这种情况下我不需要额外的 execute() 方法。
关于我如何获得这个 ruby 有什么建议吗?
谢谢!
编辑: 为多个字符串添加了其他方法类型。不确定这是否也可以以优雅的方式处理。