abdul ahmad abdul ahmad - 1 year ago 71
Ruby Question

Pass method name as parameter to another method

I want to pass a method name, not a block, as a parameter to another method to use as a callback:

def one(param)
puts param

def two(param, &callback)

two('hi', :one)

Is this possible? I tried it and got an error saying I passed two parameters but only one was expected.

Answer Source

You want to use send:

def two(param, callback)
  send(callback, param)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download