# Array into list of arguments - nicest way?

**URL:** <https://www.ruby-forum.com/t/array-into-list-of-arguments-nicest-way/161103>\
**Category:** Ruby\
**Created:** [February 23, 2009, 12:45pm UTC](https://www.ruby-forum.com/t/array-into-list-of-arguments-nicest-way/161103 "2009-02-23T12:45:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2009, 12:45pm UTC](https://www.ruby-forum.com/t/array-into-list-of-arguments-nicest-way/161103/1 "2009-02-23T12:45:17Z")

</div>

hey all

I want to pass the contents of an array to a method, to be interpreted  
as a standard list of arguments. What’s the simplest and nicest way of  
doing this? (i can think of a few dirty hack ways but don’t want to use  
them)

eg

#have  
arr = [arg1, arg2, arg3]  
#want  
a\_method(arg1, arg2, arg3)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/array-into-list-of-arguments-nicest-way/161103/2 "2009-02-23T12:52:52Z")

</div>

Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #have  
> arr = [arg1, arg2, arg3]  
> #want  
> a\_method(arg1, arg2, arg3)

a\_method(\*arr)

Regards,  
Jan F.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/array-into-list-of-arguments-nicest-way/161103/3 "2009-02-23T12:57:41Z")

</div>

On Mon, Feb 23, 2009 at 12:44 PM, Max W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> arr = [arg1, arg2, arg3]  
> #want  
> a\_method(arg1, arg2, arg3)

Look for documentation, etc for the splat operator (there’s plenty out  
there):

a\_method (\*arr)

Jesus.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/array-into-list-of-arguments-nicest-way/161103/4 "2009-02-23T13:17:15Z")

</div>

Max W. wrote:

> arr = [arg1, arg2, arg3]  
> #want  
> a\_method(arg1, arg2, arg3)

Look up the splat operator, a.k.a. unary unarray:

a\_method(\*arr)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2009, 1:29pm UTC](https://www.ruby-forum.com/t/array-into-list-of-arguments-nicest-way/161103/5 "2009-02-23T13:29:03Z")

</div>

aha, that makes sense…

thanks a lot, everyone. I’ve used the splat before at the end of  
parameter lists, eg

def a\_method(a\_thing, \*args)

But i guess i didn’t really properly understand what it does. I can’t  
find an explaanation in pickaxe, although it’s not the simplest thing to  
look for in the index (there’s no listing for ‘splat’ and a lot of  
listings for ‘\*’).

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2009, 1:37pm UTC](https://www.ruby-forum.com/t/array-into-list-of-arguments-nicest-way/161103/6 "2009-02-23T13:37:27Z")

</div>

Max W. wrote:

> But i guess i didn’t really properly understand what it does. I can’t  
> find an explaanation in pickaxe, although it’s not the simplest thing to  
> look for in the index (there’s no listing for ‘splat’ and a lot of  
> listings for ‘\*’).

[http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/)

Click on the chapter “More about Methods”

Scroll down to the section headed “Expanding Arrays in Method Calls”

It could be indexed better 🙂

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/array-into-list-of-arguments-nicest-way/161103/7 "2009-02-23T13:55:24Z")

</div>

Brian C. wrote:

> Max W. wrote:
> 
> > But i guess i didn’t really properly understand what it does. I can’t  
> > find an explaanation in pickaxe, although it’s not the simplest thing to  
> > look for in the index (there’s no listing for ‘splat’ and a lot of  
> > listings for ‘\*’).
> 
> [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/)
> 
> Click on the chapter “More about Methods”
> 
> Scroll down to the section headed “Expanding Arrays in Method Calls”
> 
> It could be indexed better 🙂

Cool. I’m using the paper version, it’s page 83 🙂

Thanks a lot.
