Project

General

Profile

Actions

Bug #20041

open

Array destructuring and default values in parameters

Added by tenderlovemaking (Aaron Patterson) 12 months ago. Updated 8 months ago.


Description

It's possible to set the default value of a parameter to a previous parameter. For example:

def foo(a, b = a)
  b
end

foo([1, 2]) # => [1, 2]

However, if the parameters are destructured, the destructring happens after default parameter assignment. For example:

def foo((x, y), b = x)
  [x, y, b]
end

foo([1, 2]) # => [1, 2, nil]

Is this expected behavior? I would have expected the parameters to be "evaluated" from left to right, and the array destructuring to happen before the default parameter assignment.

Thanks!

Actions

Also available in: Atom PDF

Like0
Like1Like0Like0Like0