Breaking apart arrays for arguments?

K

Kyle Schmitt

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
 
T

Thomas B.

Kyle said:
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.
 
K

Kyle Schmitt

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)
 
T

Thomas B.

Kyle said:
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.
 
K

Kyle Schmitt

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.

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.
 
K

Kyle Schmitt

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)
 
T

Todd Benson

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)

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
 
K

Kyle Schmitt

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
 
T

Todd Benson

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.

Same numbers as Kyle.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top