rubyparallel-assignment

Parallel assignment with parentheses and splat operator


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.


Solution

  • 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.