From: brabuhr@... Date: 2007-04-29T01:55:16+09:00 Subject: Re: code block which allows dynamical numbers of arguments On 4/28/07, anansi wrote: > let's pretend I have a codeblock like this: > > #begin > test = lambda {|c,d| @check = c if object.status == d} > test.call("GoodBoy",12) > #end > > So you see if object.status is 12 @check will be set to "Goodboy". > > But how can I make the block test to allow more than 2 arguments and > that any further argument will be takes as further conditional statement? > > Like: > > test = lambda {|c,d,e| @check = c if object.status == d or object.status > == e} > test.call("GoodBoy",24,11) > > This should just happen automatically so I wanna call test sometimes > with 2, but sometimes also with more arguments and it shall be extended > as shown above. > > Is there any way in ruby to do so? test = lambda {|c,d| @check = c if 12 == d}=> # test.call("GoodBoy",11) => nil test.call("GoodBoy",12) => "GoodBoy" test = lambda {|*args| @check = args[0] if 12 == args[1]} => # test.call("GoodBoy",11) => nil test.call("GoodBoy",12) => "GoodBoy" test = lambda {|*args| @check = args[0] if args[1..-1].include? 12} => # test.call("GoodBoy",11) => nil test.call("GoodBoy",11,13) => nil test.call("GoodBoy",11,13,12) => "GoodBoy"