[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Breaking apart arrays for arguments?

Kyle Schmitt

9/23/2008 4:39:00 PM

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

8 Answers

TPReal

9/23/2008 4:42:00 PM

0

Kyle Schmitt 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.
--
Posted via http://www.ruby-....

Kyle Schmitt

9/23/2008 5:09:00 PM

0

On Tue, Sep 23, 2008 at 11:41 AM, Thomas B. <tpreal@gmail.com> 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)

TPReal

9/23/2008 6:28:00 PM

0

Kyle Schmitt 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.
--
Posted via http://www.ruby-....

Kyle Schmitt

9/23/2008 7:19:00 PM

0

On Tue, Sep 23, 2008 at 1:28 PM, Thomas B. <tpreal@gmail.com> wrote:
> Kyle Schmitt 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.

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.

Kyle Schmitt

9/23/2008 8:19:00 PM

0

On Tue, Sep 23, 2008 at 2:19 PM, Kyle Schmitt <kyleaschmitt@gmail.com> wrote:
>> 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.

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)

Todd Benson

9/24/2008 4:58:00 AM

0

On Tue, Sep 23, 2008 at 3:19 PM, Kyle Schmitt <kyleaschmitt@gmail.com> wrote:
> On Tue, Sep 23, 2008 at 2:19 PM, Kyle Schmitt <kyleaschmitt@gmail.com> wrote:
>>> 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.
>
> 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

Kyle Schmitt

9/24/2008 2:11:00 PM

0

On Tue, Sep 23, 2008 at 11:58 PM, Todd Benson <caduceass@gmail.com> 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

Todd Benson

9/24/2008 8:04:00 PM

0

On Wed, Sep 24, 2008 at 9:10 AM, Kyle Schmitt <kyleaschmitt@gmail.com> wrote:
> On Tue, Sep 23, 2008 at 11:58 PM, Todd Benson <caduceass@gmail.com> 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.

Same numbers as Kyle.