<分区>
在 Obj-C 中,可以使用选择器机制在运行时决定调用的方法。
Ruby 中是否有类似的东西,以便我可以在运行时将方法字符串转换为方法符号并调用它?
标签 ruby
<分区>
在 Obj-C 中,可以使用选择器机制在运行时决定调用的方法。
Ruby 中是否有类似的东西,以便我可以在运行时将方法字符串转换为方法符号并调用它?
最佳答案
你想要 send
方法:
obj.send(method_name)
, 其中method_name
可以是字符串或符号,将在 obj
上调用具有给定名称的方法.
任何你想传递给方法的参数,都可以作为附加参数给send
,即 obj.send(method_name, argument1, argument2)
.
关于 ruby 问题 : Is there any way to invoke a method like the selector in Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6955102/