# Passing array instead of individual parameters?

**URL:** <https://www.ruby-forum.com/t/passing-array-instead-of-individual-parameters/152959>\
**Category:** Ruby\
**Created:** [November 10, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/passing-array-instead-of-individual-parameters/152959 "2008-11-10T15:08:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Kruger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@Stefan\_Kruger](https://www.ruby-forum.com/u/Stefan_Kruger)\
**Post date:** [November 10, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/passing-array-instead-of-individual-parameters/152959/1 "2008-11-10T15:08:49Z")

</div>

[New to this fine language, from a Perl background]

I expected (wrongly) the following to work -

def myroutine(one, two, three)  
puts “first: #{one} second: #{two} third: #{three}”  
end

myroutine(‘brave’, ‘new’, ‘world’) # as expected

# The unexpected (to me)

myparamlist = [‘brave’, ‘new’, ‘world’]

myroutine(myparamlist) # fails

So, my question is - is there a way of calling a routine expecting N  
parameters with a list containing N elements instead?

Specifically, using the dbi module, I expected to be able to say:

sth.prepare(“insert into foo (one, two, three) values (?, ?, ?)”)  
sth.execute(my\_list\_of\_three\_values)

but instead I need to loop through the list and bind each one in turn?

Many thanks,

stefan

---

<div class="post-metadata">

**Author:** ![Stefan\_Kruger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@Stefan\_Kruger](https://www.ruby-forum.com/u/Stefan_Kruger)\
**Post date:** [November 10, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/passing-array-instead-of-individual-parameters/152959/2 "2008-11-10T15:14:42Z")

</div>

On Nov 10, 2008, at 8:05 AM, Stefan Kruger wrote:

> # The unexpected (to me)
> 
> myparamlist = [‘brave’, ‘new’, ‘world’]
> 
> myroutine(myparamlist) # fails
> 
> So, my question is - is there a way of calling a routine expecting N  
> parameters with a list containing N elements instead?

myroutine(\*myparamlist)

---

<div class="post-metadata">

**Author:** ![Stefan\_Kruger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@Stefan\_Kruger](https://www.ruby-forum.com/u/Stefan_Kruger)\
**Post date:** [November 10, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/passing-array-instead-of-individual-parameters/152959/3 "2008-11-10T15:31:48Z")

</div>

Alle Monday 10 November 2008, Stefan Kruger ha scritto:

> myroutine(myparamlist) # fails
> 
> So, my question is - is there a way of calling a routine expecting N  
> parameters with a list containing N elements instead?

myroutine(\*myparamlist)

The \*, when put in front of an array in an argument list transforms each  
element of the array in an argument to the method. It is sometimes  
called the  
“splat operator”, even if it’s not a true operator.

Stefano
