Breaking apart arrays for arguments?

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

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.

On Tue, Sep 23, 2008 at 11:41 AM, Thomas B. [email protected] wrote:

Hello. It’s the splat operator:

meth(*args_array)

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

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)

On Tue, Sep 23, 2008 at 1:28 PM, Thomas B. [email protected] 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.

On Tue, Sep 23, 2008 at 2:19 PM, Kyle S. [email protected]
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)

On Tue, Sep 23, 2008 at 3:19 PM, Kyle S. [email protected]
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

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.

On Tue, Sep 23, 2008 at 11:58 PM, Todd B. [email protected]
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

On Wed, Sep 24, 2008 at 9:10 AM, Kyle S. [email protected]
wrote:

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

Same numbers as Kyle.