1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00
Files
til/ruby/destructuring-arrays-in-blocks.md
2015-04-24 08:41:04 -05:00

39 lines
998 B
Markdown

# Destructuring Arrays In Blocks
If I am iterating over a collection of arrays (let's say tuples) and I want
to access the values of those arrays within the iteration block, I may do
something like the following:
```ruby
> a = [[1,2],[3,4],[5,6]]
> a.each { |tuple| puts "#{tuple[0]} - #{tuple[1]}" }
1 - 2
3 - 4
5 - 6
```
I can, however, use array destructuring which will not only simplify the
code, but also make it more readable, explicit, and intentional.
```ruby
> a = [[1,2],[3,4],[5,6]]
> a.each { |x_coord,y_coord| puts "#{x_coord} - #{y_coord}" }
1 - 2
3 - 4
5 - 6
```
In the same way, I can destructure arrays that are part of a hash like so:
```ruby
> h = {one: [1,2], two: [3,4], three: [5,6]}
> h.each { |key, (x_coord, y_coord)| puts "#{x_coord} - #{y_coord}" }
1 - 2
3 - 4
5 - 6
```
Note the parentheses that are placed around the part that is being
destructured. Without these parentheses, ruby will interpret `x_coord` as
the whole array value and `y_coord` will be `nil`.