Ruby将字符串转换为方法名称 [英] Ruby convert string to method name
本文介绍了Ruby将字符串转换为方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在ruby文件中定义了两种方法.
I have two methods defined in my ruby file.
def is_mandatory(string)
puts xyz
end
def is_alphabets(string)
puts abc
end
包含方法名称的数组.
An array containing the names of the methods.
methods = ["is_mandatory", "is_alphabets"]
当我执行以下操作
methods.each do |method| puts method.concat("(\"abc\")") end
它仅显示is_mandatory("abc")is_alphabets("abc")而不是实际调用该方法.
It just displays, is_mandatory("abc") is_alphabets("abc") rather than actually calling the method.
如何将字符串转换为方法名称? 任何帮助是极大的赞赏.
How can i convert the string to method name? Any help is greatly appreciated.
干杯!
推荐答案
最好的方法可能是:
methods.each { |methodName| send(methodName, 'abc') }
请参见对象#发送
这篇关于Ruby将字符串转换为方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文