#495 √ resolved
Nikos D.

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

Reported by Nikos D. | April 15th, 2008 @ 08:24 PM | 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

  • Ryan Davis

    Ryan Davis April 30th, 2008 @ 01:57 AM

      • → Assigned user changed from “” to “Wilson Bilkovich”
  • Charles L

    Charles L April 30th, 2008 @ 12:37 PM

    Runs fine for me on 234974229bf770...

  • Nikos D.

    Nikos D. April 30th, 2008 @ 01:24 PM

    Since my first report the #p has changed so it doesn't print anything if nil is passed. This fixed this particular example but things don't work always as expected. I'm not sure whether the splat op is the problem or the way the various methods handle nil input...

  • Eero Saynatkari

    Eero Saynatkari May 1st, 2008 @ 06:59 AM

      • → State changed from “new” to “resolved”

    There are a lot of weird Ruby semantics, such as the difference between #p and #puts with a nil input. I am closing this for now, please re-open or create a new one if you are able to pinpoint a failing case.

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 »