On Wed, Dec 30, 2009 at 1:00 PM, Jeff Peng <jeffpeng / netzero.net> wrote: > Jesù¸ Gabriel y GaláÏ: >> >> On Wed, Dec 30, 2009 at 12:42 PM, Fritz Trapper <ajfrenzel / web.de> wrote: >>> >>> I want to pass a reference to function as parameter to another function >>> and execute the passed function. Somthing like this: >>> >>> def executer(func) >>> unc(1) >>> end >>> >>> def test(x) >>> x >>> end >>> >>> executer(test) >>> >>> How to write this correctly in ruby? >> >> A typical way would be using blocks or procs: >> >> def executer >> yield 1 >> end >> >> executer {|x| puts "I got #{x}"} >> >> If you want something to handle around in a variable: >> >> func = lambda {|x| puts "I got #{x}"} >> executer(&func) # => I got 1 >> > > for &func, what's the "&" before "func" here? A method can receive regular parameters and a "special" block parameter. The & lets you pass a proc as that special block parameter, instead of a regular one: irb(main):033:0> executer(func) ArgumentError: wrong number of arguments (1 for 0) from (irb):33:in `executer' from (irb):33 from :0 Jesus.