Split nested block parameters

Posted . Visible to the public.

If you iterate over a collection of arrays, you can destructure the arrays within the block parameters:

movies_and_directors = [
  ['The Big Lebowski', 'Coen Brothers'],
  ['Fight Club', 'David Fincher']
]
movies_and_directors.each do |movie, director|
  # do something
end

For nested array (e.g. when you use each_with_index), you can use parentheses for destructuring:

movies_and_directors.each_with_index do |(movie, director), index|
  # do something
end
Profile picture of Tobias Kraze
Tobias Kraze
Last edit
Keywords
assignment
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2010-09-09 15:34)