The zip method takes two arrays and pairs their elements by matching indexes, creating a new array of these pairs.
Execution Sample
Ruby
a = [1, 2, 3]
b = ['a', 'b', 'c']
result = a.zip(b)
puts result.inspect
This code pairs elements from arrays a and b by their index and prints the combined array.
Execution Table
Step
Action
Index
Element from a
Element from b
Pair Created
Result Array So Far
1
Pair elements
0
1
'a'
[1, 'a']
[[1, 'a']]
2
Pair elements
1
2
'b'
[2, 'b']
[[1, 'a'], [2, 'b']]
3
Pair elements
2
3
'c'
[3, 'c']
[[1, 'a'], [2, 'b'], [3, 'c']]
4
Return result
-
-
-
-
[[1, 'a'], [2, 'b'], [3, 'c']]
💡 All elements paired by index; zip completes when shortest array ends.
Variable Tracker
Variable
Start
After 1
After 2
After 3
Final
a
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
b
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
result
[]
[[1, 'a']]
[[1, 'a'], [2, 'b']]
[[1, 'a'], [2, 'b'], [3, 'c']]
[[1, 'a'], [2, 'b'], [3, 'c']]
Key Moments - 3 Insights
What happens if the arrays have different lengths?
Zip pairs elements only up to the shortest array length, so extra elements in the longer array are ignored (see execution_table exit_note).
Does zip modify the original arrays?
No, zip creates and returns a new array of pairs without changing the original arrays, as shown by variable_tracker where 'a' and 'b' stay the same.
What is the structure of each element in the result?
Each element in the result is an array containing one element from each input array paired by index, e.g., [1, 'a'] (see execution_table Pair Created column).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the pair created?
A[1, 'a']
B[3, 'c']
C[2, 'b']
D['b', 2]
💡 Hint
Check the Pair Created column in execution_table row 2.
At which step does the zip method finish pairing elements?
AStep 4
BStep 3
CStep 1
DStep 2
💡 Hint
Look at the exit_note and the last row in execution_table.
If array b was shorter, how would the result array change?
AIt would raise an error
BIt would pair only up to the length of b
CIt would pair all elements of a and fill missing with nil
DIt would ignore array a
💡 Hint
Refer to key_moments about different lengths and exit_note in execution_table.
Concept Snapshot
zip method pairs elements from two arrays by their index
Returns a new array of paired elements
Pairs only up to shortest array length
Original arrays remain unchanged
Useful for combining related data side-by-side
Full Transcript
The zip method in Ruby takes two arrays and combines them by pairing elements at the same index. It creates a new array where each element is an array containing one element from each input array. The pairing stops when the shortest array ends, so extra elements in a longer array are ignored. The original arrays are not changed. This is useful when you want to combine related data from two lists side-by-side. For example, pairing numbers with letters by their positions. The execution table shows each step pairing elements and building the result array. The variable tracker confirms the original arrays stay the same while the result grows. Remember, zip returns a new combined array without modifying inputs.