ruby 问题 : Is there any way to invoke a method like the selector in Objective-C

标签 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/

相关文章:

ruby - 适用于 ruby​​ 的 aws-sdk。如何为实例分配公共(public)IP地址?

ruby - RVM 从本地源安装 ruby

arrays - Ruby Koans - 数组填充方法会影响它的返回方式

ruby-on-rails - 如何允许嵌套属性的强参数?

ruby - 解析文件时如何找到完成百分比?

ruby-on-rails - 无方法错误 : undefined method `methodname' for #<memory location>

Ruby:如何在不指向同一对象的情况下复制变量?

ruby - 如何简洁地将文本附加到文件

ruby-on-rails - Rails link_to 在页面加载时生成错误

python - Beautiful Soup for Ruby 最接近的等价物是什么?