Project

General

Profile

Actions

Bug #12860

closed

Splatting an argument does not obey left-to-right execution order

Added by headius (Charles Nutter) over 7 years ago. Updated about 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:77701]

Description

Ruby evaluates arguments left to right, but it does not appear to handle construction of the eventual argument list from left to right.

Take this example:

def foo(*args)
  p args
end

ary = [1,2]

foo(*ary, ary.shift)

With left-to-right execution, the ary value should be splatted (1, 2), and THEN shifted (1) producing args == [1, 2, 1].

However, on MRI, the shift occurs before the splat, so args == [2, 1].

This is counter-intuitive. At the moment in time the splat is encountered, ary is still [1, 2]. So the first two arguments should be (1, 2). THEN the shift happens, producing a third argument of (1).

This affects JRuby running Rails because they have a small piece of code that depends on this unusual behavior: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/callbacks.rb#L411-L425

This code appears to have been introduced into Rails recently, and I will file a separate issue to change it to be more explicit about ordering.


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #16504: `foo(*args, &args.pop)` should pass all elements of argsClosedjeremyevans0 (Jeremy Evans)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0