Parallel assignment with parentheses and splat operator

114 Views Asked by At

I got this:

x,(y,z)=1,*[2,3]

x # => 1
y # => 2
z # => nil

I want to know why z has the value nil.

1

There are 1 best solutions below

3
Yu Hao On BEST ANSWER
x, (y, z) = 1, *[2, 3]

The splat * on the right side is expanded inline, so it's equivalent to:

x, (y, z) = 1, 2, 3

The parenthesized list on the left side is treated as nested assignment, so it's equivalent to:

x = 1
y, z = 2

3 is discarded, while z gets assigned to nil.