#495 √ resolved
Nikos Dimitrakopoulos

[BUG] Weird behaviour from * (splat) operator when passed to a method.

Reported by Nikos Dimitrakopoulos | April 15th, 2008 @ 11:24 AM | in 1.0 preview

First of all my apologies for two things:

a) for using a more clear example

b) for pasting so much info here

require 'enumerator'
p1,p2 = (0..4).to_a.sort_by{rand}, (0..4).to_a.sort_by{rand}
nb = Array.new(p1.size){[]}
(p1 + p1[0..1]).each_cons(3){|l,m,r| nb[m] += [l,r] }
(p2 + p2[0..1]).each_cons(3){|l,m,r| nb[m] += [l,r] }
nb.map{ |*args| p *args; p args.shift; p *args }

Here are the results from different ruby implementations:

MRI:

[3, 1, 1, 4]

[3, 1, 1, 4]

[0, 4, 3, 0]

[0, 4, 3, 0]

[4, 3, 4, 3]

[4, 3, 4, 3]

...

=> [nil, nil, nil, nil, nil]

Ruby1.9:

[3, 2, 4, 3]

[3, 2, 4, 3]

[2, 4, 2, 4]

[2, 4, 2, 4]

[0, 1, 3, 1]

[0, 1, 3, 1]

...

=> [nil, nil, nil, nil, nil]

JRuby 1.1:

[2, 4, 4, 3]

[2, 4, 4, 3]

[3, 2, 2, 4]

[3, 2, 2, 4]

[1, 0, 3, 1]

[1, 0, 3, 1]

...

=> [nil, nil, nil, nil, nil]

Rubinius:

[1, 2, 2, 1]

[1, 2, 2, 1]

nil

[4, 0, 0, 3]

[4, 0, 0, 3]

nil

[0, 3, 4, 0]

[0, 3, 4, 0]

nil

...

=> [nil, nil, nil, nil, nil]

Bug for these examples the behaviour is the same...

Any thoughts would be welcome.

You can also check out #363 to see how I found this and why this can and is a serious problem :) (i will update that ticket soon)

Comments and changes to this ticket

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »