如何在Ruby中动态调用Proc?

BA

是否有proc的method.send等效项?例如:

def s a
a + 1
end
b = "s"
send b.to_sym,10 #=> 11

有这样的东西吗?

p = Proc.new{|s| s + 1}
d = "p"
*call d.to_sym,10 *

编辑:作为对mudasobwa的回答,我需要从一系列方法中调用Procs。例如:

ss = ["a","b","c","d"] 

在这种情况下可以吗?

阿列克谢·马蒂什金(Alexei Matiushkin)

对于此特定示例:

p = Proc.new{|s| s + 1}
d = "p"
#⇒ *call d.to_sym,10 *

这将是:

binding.local_variable_get(d).(10)
#⇒ 11

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章