# Variable argument number and behaviour \[Newbie\]

**URL:** <https://www.ruby-forum.com/t/variable-argument-number-and-behaviour-newbie/108300>\
**Category:** Ruby\
**Created:** [July 1, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/variable-argument-number-and-behaviour-newbie/108300 "2007-07-01T23:53:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![creativelogic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@creativelogic](https://www.ruby-forum.com/u/creativelogic)\
**Post date:** [July 1, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/variable-argument-number-and-behaviour-newbie/108300/1 "2007-07-01T23:53:31Z")

</div>

Hello Everyone,

I’m trying to make a function take arguments in a particular way.

This is what I want to create:

#Variable argument testing  
def function(\*sometimes)

if sometimes.nil?  
print “Not given!”  
else  
print “Here!”  
end

end

function  
[EOF]

As I understand it, the \* operator bundles the argument into an array,  
so that if test returns true. I know that I can use “if  
sometimes.length==0” to get the same behaviour, but that is rather  
obscure compared to the first version.

Is there some other way to set up the argument list? (Alas, I have found  
nothing in the documentation or the Forum.)

Thank you,  
-Ryan

---

<div class="post-metadata">

**Author:** ![creativelogic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@creativelogic](https://www.ruby-forum.com/u/creativelogic)\
**Post date:** [July 2, 2007, 12:06am UTC](https://www.ruby-forum.com/t/variable-argument-number-and-behaviour-newbie/108300/2 "2007-07-02T00:06:23Z")

</div>

Alle domenica 1 luglio 2007, Ryan Allan ha scritto:

> ```
> print "Not given!"
> 
> ```
> 
> so that if test returns true. I know that I can use “if  
> sometimes.length==0” to get the same behaviour, but that is rather  
> obscure compared to the first version.
> 
> Is there some other way to set up the argument list? (Alas, I have found  
> nothing in the documentation or the Forum.)
> 
> Thank you,  
> -Ryan

I can’t think of any other way to make a method take any number of  
arguments,  
but remember that there’s a shorter way to tell whether an array is  
empty:  
the empty? method:

def function(\*sometimes)  
if sometimes.empty?  
print “Not given!”  
else  
print “Here!”  
end  
end

This way, it’s as clear as what you wanted to use. Besides, even if it  
had  
been possible, your approach would have had a problem: you wouldn’t have  
been  
able to distinguish the case of a single nil argument from the case of  
no  
arguments:

function(nil)

function()

The correct approach solves this problem: if no arguments are passed,  
sometimes is empty (empty? returns true); otherwise it’s not empty, even  
if  
all arguments are nil.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![creativelogic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@creativelogic](https://www.ruby-forum.com/u/creativelogic)\
**Post date:** [July 2, 2007, 12:13am UTC](https://www.ruby-forum.com/t/variable-argument-number-and-behaviour-newbie/108300/3 "2007-07-02T00:13:07Z")

</div>

Thank you Stefano.  
You’re right; the empty? method is nicely obvious.  
I do have a further question, however. You’ve highlighted a problem  
with my original idea, but I don’t understand it.

> Besides, even if it  
> had been possible, your approach would have had a problem: you wouldn’t have  
> been able to distinguish the case of a single nil argument from the case of  
> no arguments:
> 
> function(nil)
> 
> function()
> 
> The correct approach solves this problem: if no arguments are passed,  
> sometimes is empty (empty? returns true); otherwise it’s not empty, even  
> if all arguments are nil.

What is the difference between no arguments and a single nil argument?  
It seems to me that the cases are logically equivalent. Ruby treats them  
separately?

Thanks again,  
-Ryan

---

<div class="post-metadata">

**Author:** ![creativelogic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@creativelogic](https://www.ruby-forum.com/u/creativelogic)\
**Post date:** [July 2, 2007, 12:21am UTC](https://www.ruby-forum.com/t/variable-argument-number-and-behaviour-newbie/108300/4 "2007-07-02T00:21:42Z")

</div>

Thank you David. That makes much more sense now.

---

<div class="post-metadata">

**Author:** ![creativelogic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@creativelogic](https://www.ruby-forum.com/u/creativelogic)\
**Post date:** [July 2, 2007, 12:19am UTC](https://www.ruby-forum.com/t/variable-argument-number-and-behaviour-newbie/108300/5 "2007-07-02T00:19:25Z")

</div>

Hi –

On Mon, 2 Jul 2007, Ryan Allan wrote:

> > function(nil)
> > 
> > function()
> > 
> > The correct approach solves this problem: if no arguments are passed,  
> > sometimes is empty (empty? returns true); otherwise it’s not empty, even  
> > if all arguments are nil.
> 
> What is the difference between no arguments and a single nil argument?  
> It seems to me that the cases are logically equivalent. Ruby treats them  
> separately?

Yes, because nil is an object.

def meth(x)  
p x  
end

meth(1) # 1  
meth(nil) # nil  
meth # ArgumentError: wrong number of arguments

David
