B A B A - 1 year ago 70
Ruby Question

How can i dynamically call a Proc in Ruby?

Is there a method.send equivalent for proc?

def s a
a + 1
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 *

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

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

Is it possible in this case?

Answer Source

For this particular example:

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

It would be:

#⇒ 11
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download