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
.
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
.
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
.
*
on the right side...". Parallel assignment causes x
to be set equal to 1
, (y,z)
set equal to 2
and so on, and if (y,z) = 2
, then y #=> 2; z #=> nil
. –
Bin © 2022 - 2024 — McMap. All rights reserved.
x, (y, z), u = 1, *[2, 3]
thenx #=> 1; y #=> 2; z #=> nil; u #=> 3
. – Bin