Skip to content
Justice Tech School

Destructuring and Splat

Assign several values at once, use * to collect the rest and ** for keywords.

Example
a, b = 1, 2
a, b = b, a
first, *rest = [1, 2, 3]
puts a, b, first, rest.inspect
x, (y, z) = 1, [2, 3]
puts y + z

Result

2
1
1
[2, 3]
5

Exercise

Collect the rest.

a, r = [1, 2, 3]

Quiz Test

How do you swap a and b?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.