Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 206
Devashish Bhardwaj Avatar asked Jun 06 '15 03:06

Devashish Bhardwaj


1 Answers

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.

like image 140
Yu Hao Avatar answered Oct 11 '22 23:10

Yu Hao