Recall & Review
beginner
What does the
zip method do in Ruby?The
zip method combines elements from two or more arrays into a single array of arrays, pairing elements with the same index together.Click to reveal answer
intermediate
How does
zip handle arrays of different lengths?If arrays have different lengths,
zip fills missing elements with nil for shorter arrays.Click to reveal answer
beginner
Example: What is the result of
[1, 2, 3].zip([4, 5, 6])?The result is
[[1, 4], [2, 5], [3, 6]]. Each element from the first array is paired with the element at the same index from the second array.Click to reveal answer
intermediate
Can
zip combine more than two arrays?Yes,
zip can combine multiple arrays by passing them as arguments. It pairs elements from all arrays by their index.Click to reveal answer
advanced
How can you use
zip with a block?You can pass a block to
zip to process each combined array immediately. The block receives each paired array as an argument.Click to reveal answer
What does
[1, 2].zip([3, 4]) return?✗ Incorrect
zip pairs elements by index, so it returns [[1, 3], [2, 4]].What happens if arrays have different lengths in
zip?✗ Incorrect
Ruby fills missing elements with
nil when arrays have different lengths.How do you combine three arrays
a, b, and c using zip?✗ Incorrect
You pass multiple arrays as arguments:
a.zip(b, c).What does this code output?<br>
[1, 2].zip([3, 4]) { |x| p x }✗ Incorrect
The block prints each pair, but
zip returns the original array when a block is given.Which method pairs elements from arrays by their index?
✗ Incorrect
zip pairs elements from arrays by their index.Explain how the
zip method works to combine arrays in Ruby.Think about matching elements from each array by their position.
You got /3 concepts.
Describe how you can use a block with
zip and what effect it has.Consider what happens inside the block and the method's return value.
You got /3 concepts.