From: Robert Klemme Date: 2008-10-29T19:41:10+09:00 Subject: Re: lambda and multiple arguments: how?! 2008/10/28 nvp : > On Tue, Oct 28, 2008 at 1:36 PM, David A. Black wrote: >> You could call it as: code.call(pid,*params), > > This didn't work for me with: > > lambda { > |pid,exit_code,ident| > # ... > } > > What am I missing here? You need to adjust the parameter list to the arity of the block. Here's a way how to do it 11:38:30 Temp$ cat lambda.rb @code = lambda {|a,b,c| printf "R: %p %p %p\n",a,b,c} def run1(*params) ar = @code.arity if ar >= 0 params = params[0...@code.arity] params.concat(Array.new(ar - params.size)) end @code[*params] @code.call(*params) end def run2(*params) params.unshift :pid ar = @code.arity if ar >= 0 params = params[0...@code.arity] params.concat(Array.new(ar - params.size)) end @code[*params] @code.call(*params) end run1 1 run1 1,2 run1 1,2,3 run2 1 run2 1,2 run2 1,2,3 11:38:32 Temp$ ruby lambda.rb R: 1 nil nil R: 1 nil nil R: 1 2 nil R: 1 2 nil R: 1 2 3 R: 1 2 3 R: :pid 1 nil R: :pid 1 nil R: :pid 1 2 R: :pid 1 2 R: :pid 1 2 R: :pid 1 2 11:38:33 Temp$ Btw, IMHO you have a slight design inconsistency in your code: the client registers a lambda with a certain pid so it *knows* the pid already. But still you provide it to the block when calling it. That seems superfluous because even if the pid is stored in a local variable the block can access it because it is a closure: irb(main):001:0> def bl irb(main):002:1> pid = Time.now.to_i # random irb(main):003:1> lambda { puts "pid is #{pid}" } irb(main):004:1> end => nil irb(main):005:0> x = bl => # irb(main):006:0> x[] pid is 1225276874 => nil irb(main):007:0> x[] pid is 1225276874 => nil irb(main):008:0> x[] pid is 1225276874 => nil irb(main):009:0> x[] pid is 1225276874 => nil irb(main):010:0> Kind regards robert -- remember.guy do |as, often| as.you_can - without end