# Code block which allows dynamical numbers of arguments

**URL:** <https://www.ruby-forum.com/t/code-block-which-allows-dynamical-numbers-of-arguments/93173>\
**Category:** Ruby\
**Created:** [April 28, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/code-block-which-allows-dynamical-numbers-of-arguments/93173 "2007-04-28T18:26:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anansi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9fc348/32.png) [@anansi](https://www.ruby-forum.com/u/anansi)\
**Post date:** [April 28, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/code-block-which-allows-dynamical-numbers-of-arguments/93173/1 "2007-04-28T18:26:33Z")

</div>

Hi,  
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)

or with 4 arguments:

test = lambda {|c,d,e,f| @check = c if object.status == d or  
object.status == e or object.status == f}  
test.call(“GoodBoy”,82,31,40)

so you see what I mean?

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?

–  
greets  
(  
)  
(  
/\ .-"""-. /  
//\/ , //\  
|/| ,;;;;;, |/|  
//\;-"""-;///\  
// / . / \  
(| ,-_| \ | / |_-, |)  
//` __\.-.-./__ `\  
// /.-(() ())-.\ \  
(\ |) ‘—’ (| /)  
`(| |)`  
jgs ) (/

one must still have chaos in oneself to be able to give birth to a  
dancing star

---

<div class="post-metadata">

**Author:** ![anansi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9fc348/32.png) [@anansi](https://www.ruby-forum.com/u/anansi)\
**Post date:** [April 28, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/code-block-which-allows-dynamical-numbers-of-arguments/93173/2 "2007-04-28T18:56:36Z")

</div>

On 4/28/07, anansi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> that any further argument will be takes as further conditional statement?
> 
> Is there any way in ruby to do so?

test = lambda {|c,d| @check = c if 12 == d}=\>  
#Proc:0xb7df9528@:14(irb)  
test.call(“GoodBoy”,11)  
=\> nil  
test.call(“GoodBoy”,12)  
=\> “GoodBoy”

test = lambda {|\*args| @check = args[0] if 12 == args[1]}  
=\> #Proc:0xb7ddb820@:21(irb)  
test.call(“GoodBoy”,11)  
=\> nil  
test.call(“GoodBoy”,12)  
=\> “GoodBoy”

test = lambda {|\*args| @check = args[0] if args[1…-1].include? 12}  
=\> #Proc:0xb7db8154@:28(irb)  
test.call(“GoodBoy”,11)  
=\> nil  
test.call(“GoodBoy”,11,13)  
=\> nil  
test.call(“GoodBoy”,11,13,12)  
=\> “GoodBoy”

---

<div class="post-metadata">

**Author:** ![anansi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9fc348/32.png) [@anansi](https://www.ruby-forum.com/u/anansi)\
**Post date:** [April 28, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/code-block-which-allows-dynamical-numbers-of-arguments/93173/3 "2007-04-28T18:57:37Z")

</div>

You’d want to use the excellent splat (\*) operator for that.

lambda { |check, \*statuses| @check = check if statuses.include?  
(object.status) }

To get an idea of what happens, see the following:

irb(main):011:0\> test = lambda { |c, \*args| puts c.inspect; puts  
args.inspect }  
=\> #Proc:0x00044d40@:11(irb)  
irb(main):012:0\> test.call(‘test’)  
“test”  
[]  
=\> nil  
irb(main):013:0\> test.call(‘test’, 1)  
“test”  
[1]  
=\> nil  
irb(main):014:0\> test.call(‘test’, 1,2,3,4)  
“test”  
[1, 2, 3, 4]  
=\> nil

---

<div class="post-metadata">

**Author:** ![anansi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9fc348/32.png) [@anansi](https://www.ruby-forum.com/u/anansi)\
**Post date:** [April 28, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/code-block-which-allows-dynamical-numbers-of-arguments/93173/4 "2007-04-28T19:10:37Z")

</div>

thank you both 😃 worked like a charm

–  
greets  
(  
)  
(  
/\ .-"""-. /  
//\/ , //\  
|/| ,;;;;;, |/|  
//\;-"""-;///\  
// / . / \  
(| ,-_| \ | / |_-, |)  
//` __\.-.-./__ `\  
// /.-(() ())-.\ \  
(\ |) ‘—’ (| /)  
`(| |)`  
jgs ) (/

one must still have chaos in oneself to be able to give birth to a  
dancing star
