Skip to content

Commit

Permalink
Add docs for unlimited block unpacking
Browse files Browse the repository at this point in the history
Unlimited block unpacking was introduced in crystal-lang/crystal#11597
  • Loading branch information
straight-shoota committed Oct 9, 2023
1 parent 67e05a4 commit 8f61669
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/syntax_and_semantics/blocks_and_procs.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,35 @@ end

That means that any type that responds to `[]` with integers can be unpacked in a block parameter.

Parameter unpacking can be nested.

```crystal
ary = [
{1, {2, {3, 4}}},
]
ary.each do |(w, (x, (y, z)))|
w # => 1
x # => 2
y # => 3
z # => 4
end
```

Splat parameters are supported.

```crystal
ary = [
[1, 2, 3, 4, 5],
]
ary.each do |(x, *y, z)|
x # => 1
y # => [2, 3, 4]
z # => 5
end
```

For [Tuple](https://crystal-lang.org/api/Tuple.html) parameters you can take advantage of auto-splatting and do not need parentheses:

```crystal
Expand Down

0 comments on commit 8f61669

Please sign in to comment.