# Breaking apart arrays for arguments?

**URL:** <https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330>\
**Category:** Ruby\
**Created:** [September 23, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330 "2008-09-23T18:39:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [September 23, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330/1 "2008-09-23T18:39:53Z")

</div>

Is there a way to break apart an array to use it as arguments to  
another method? I would’ve sworn I’ve seen it, but I can’t recall how  
to do it.

Take the idea of trying to prepend to an array using unshift.

george=Array.new(10,0)  
=\> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

george.unshift(1,1,1,1,1,1,1,1,1,1)  
=\> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  
#silly

george.unshift(Array.new(10,2))  
=\> [[2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,  
0, 0, 0, 0, 0, 0, 0, 0, 0]  
#close-ish

george.unshift(Array.new(10,3)).flatten!  
=\> [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1,  
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  
#works, but it just feels wrong

Thanks,  
Kyle

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [September 23, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330/2 "2008-09-23T18:41:47Z")

</div>

Kyle S. wrote:

> Is there a way to break apart an array to use it as arguments to  
> another method? I would’ve sworn I’ve seen it, but I can’t recall how  
> to do it.

Hello. It’s the splat operator:

meth(\*args\_array)

TPR.

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [September 23, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330/3 "2008-09-23T19:51:44Z")

</div>

On Tue, Sep 23, 2008 at 11:41 AM, Thomas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello. It’s the splat operator:
> 
> meth(\*args\_array)

Thanks. Evidence there isn’t enough coffee in my office 🙂

Although it’s odd. I would have expected using the splat operator to  
provide better performance than flatten, doesn’t seem to be the case.  
I guess I need to read up on the underpinnings of ruby’s splat.

require ‘benchmark’  
$size=1024  
$runs=1024

Benchmark.bm do  
|bm|  
bm.report(“unshift pure”) do  
unshifter=Array.new($size){0}  
1.upto($runs) do  
|index|  
unshifter.unshift(\*Array.new($size){index})  
end  
end

bm.report(“unshift, flatten at end”) do  
unshifter=Array.new($size){0}  
1.upto($runs) do  
|index|  
unshifter.unshift(Array.new($size){index})  
end  
unshifter.flatten!  
end  
end

```
  user system total real

```

unshift pure 4.060000 0.660000 4.720000 ( 5.908041)  
unshift, flatten at end 1.660000 0.550000 2.210000 ( 2.431595)

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [September 23, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330/4 "2008-09-23T21:20:07Z")

</div>

On Tue, Sep 23, 2008 at 1:28 PM, Thomas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> unshift pure 1.469000 0.046000 1.515000 ( 1.547000)  
> unshift, flatten at end 3.156000 1.047000 4.203000 ( 4.250000)
> 
> RUBY\_VERSION 1.8.6, RUBY\_PATCHLEVEL 287, RUBY\_RELEASE\_DATE 2008-08-11  
> Windows One-Click Installer
> 
> That’s strange.
> 
> TPR.

Very odd indeed, because those are more like the numbers I would have  
expected!  
then again I’m running an old old version, the official binary for  
CentOS:  
ruby 1.8.5 (2006-08-25)

Guess I need to compile something better.

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [September 23, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330/5 "2008-09-23T22:20:00Z")

</div>

On Tue, Sep 23, 2008 at 2:19 PM, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > TPR.
> 
> Very odd indeed, because those are more like the numbers I would have expected!  
> then again I’m running an old old version, the official binary for CentOS:  
> ruby 1.8.5 (2006-08-25)
> 
> Guess I need to compile something better.

Even wierder now. I grabbed the latest 1.9 source from subversion  
compiled & installed.

The 1.9 times are better than I was getting with 1.8.5, but unshift,  
flatten was still hugely faster… I’m gonna have to try this again  
at home with a box that has 1.8.6 on it.

with 1.9 from subversion  
user system total real  
unshift pure 3.900000 0.110000 4.010000 ( 4.691366)  
unshift, flatten at end 0.430000 0.010000 0.440000 ( 0.501283)

with 1.8.5  
user system total real  
unshift pure 4.050000 0.670000 4.720000 ( 5.635800)  
unshift, flatten at end 1.660000 0.570000 2.230000 ( 2.413984)

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [September 24, 2008, 7:32am UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330/6 "2008-09-24T07:32:30Z")

</div>

On Tue, Sep 23, 2008 at 3:19 PM, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > That’s strange.  
> > > compiled & installed.  
> > > with 1.8.5  
> > > user system total real  
> > > unshift pure 4.050000 0.670000 4.720000 ( 5.635800)  
> > > unshift, flatten at end 1.660000 0.570000 2.230000 ( 2.413984)

I get the same numbers. Not knowing the internal workings of Ruby, it  
seems pretty obvious that this has to do with the fact you are not  
running flattening 1024 times, but you are splatting that many times.

Todd

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [September 23, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330/7 "2008-09-23T20:28:22Z")

</div>

Kyle S. wrote:

> ```
> user system total real
> 
> ```
> 
> unshift pure 4.060000 0.660000 4.720000 ( 5.908041)  
> unshift, flatten at end 1.660000 0.550000 2.210000 ( 2.431595)

Hmmmm.

I run your code without any changes, and I got:

```
  user system total real

```

unshift pure 1.469000 0.046000 1.515000 ( 1.547000)  
unshift, flatten at end 3.156000 1.047000 4.203000 ( 4.250000)

RUBY\_VERSION 1.8.6, RUBY\_PATCHLEVEL 287, RUBY\_RELEASE\_DATE 2008-08-11  
Windows One-Click Installer

That’s strange.

TPR.

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [September 24, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330/8 "2008-09-24T16:41:45Z")

</div>

On Tue, Sep 23, 2008 at 11:58 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I get the same numbers. Not knowing the internal workings of Ruby, it  
> seems pretty obvious that this has to do with the fact you are not  
> running flattening 1024 times, but you are splatting that many times.
> 
> Todd

Sames numbers as me? Or as Tom?

And it makes sense as far as the splatting that many times. It just  
seems. Well, a little weird.

–Kyle

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [September 24, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/breaking-apart-arrays-for-arguments/149330/9 "2008-09-24T22:05:35Z")

</div>

On Wed, Sep 24, 2008 at 9:10 AM, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> And it makes sense as far as the splatting that many times. It just  
> seems. Well, a little weird.

Same numbers as Kyle.
