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:
I can, however, use array destructuring which will not only simplify the code, but also make it more readable, explicit, and intentional.
In the same way, I can destructure arrays that are part of a hash like so:
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
.
Last updated