B A B A - 8 days ago 6
Ruby Question

How can i dynamically call a Proc in Ruby?

Is there a method.send equivalent for proc?
eg:

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


Is there something like this?

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


EDIT:
In response to mudasobwa's answer
I need to call the Procs from an array of methods.
eg:

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


Is it possible in this case?

Answer

For this particular example:

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

It would be:

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