📖 Scenario: You are organizing a small event and have two lists: one with guest names and another with their meal choices. You want to pair each guest with their meal choice to prepare name cards.
🎯 Goal: Build a Ruby program that combines two arrays using zip to pair each guest with their meal choice.
📋 What You'll Learn
Create an array called guests with the exact values: "Alice", "Bob", "Charlie"
Create an array called meals with the exact values: "Salad", "Steak", "Pasta"
Create a variable called guest_meal_pairs that uses guests.zip(meals) to combine the arrays
Print the guest_meal_pairs array
💡 Why This Matters
🌍 Real World
Combining lists of related information, like pairing guests with their meal choices, is common in event planning and data organization.
💼 Career
Understanding how to combine arrays helps in data processing, report generation, and building user-friendly displays in many programming jobs.
Progress0 / 4 steps
1
Create the guests array
Create an array called guests with these exact values: "Alice", "Bob", "Charlie"
Ruby
Hint
Use square brackets [] to create an array and separate names with commas.
2
Create the meals array
Create an array called meals with these exact values: "Salad", "Steak", "Pasta"
Ruby
Hint
Use square brackets [] to create an array and separate meal names with commas.
3
Combine the arrays using zip
Create a variable called guest_meal_pairs that uses guests.zip(meals) to combine the two arrays
Ruby
Hint
Use the zip method on the guests array and pass meals as argument.
4
Print the combined pairs
Print the guest_meal_pairs array to show the paired guests and meals
Ruby
Hint
Use puts guest_meal_pairs.inspect to display the combined array.
Practice
(1/5)
1. What does the Ruby method zip do when used with two arrays?
easy
A. Concatenates the two arrays into one long array.
B. Combines elements from both arrays into pairs based on their positions.
C. Removes duplicate elements from both arrays.
D. Sorts both arrays in ascending order.
Solution
Step 1: Understand the purpose of zip
The zip method pairs elements from arrays by their index positions.
Step 2: Compare with other array operations
Unlike sorting or concatenation, zip creates pairs, not a sorted or merged list.
Final Answer:
Combines elements from both arrays into pairs based on their positions. -> Option B
Quick Check:
Zip pairs elements by position = D [OK]
Hint: Zip pairs elements by index from arrays [OK]
Common Mistakes:
Thinking zip sorts arrays
Assuming zip concatenates arrays
Confusing zip with removing duplicates
2. Which of the following is the correct syntax to zip two arrays a and b in Ruby?
easy
A. a.zip(b)
B. zip(a, b)
C. a.zip = b
D. zip a, b
Solution
Step 1: Recall Ruby method call syntax
In Ruby, methods are called on objects with dot notation, so a.zip(b) is correct.
Step 2: Identify incorrect syntax
The other options do not follow Ruby's method call syntax and will cause errors.
Final Answer:
a.zip(b) -> Option A
Quick Check:
Method call with dot and parentheses = A [OK]
Hint: Use dot notation: array.zip(other_array) [OK]
Common Mistakes:
Using zip as a standalone function
Assigning zip like a variable
Omitting parentheses incorrectly
3. What is the output of the following Ruby code?
a = [1, 2, 3]
b = ['a', 'b', 'c']
result = a.zip(b)
puts result.inspect
medium
A. [[1, 2, 3], ['a', 'b', 'c']]
B. [1, 'a', 2, 'b', 3, 'c']
C. [[1, 'a'], [2, 'b'], [3, nil]]
D. [[1, 'a'], [2, 'b'], [3, 'c']]
Solution
Step 1: Understand zip pairing
The method pairs elements by index: 1 with 'a', 2 with 'b', 3 with 'c'.
Step 2: Check output format
The result is an array of arrays, each inner array holding one pair.
Final Answer:
[[1, 'a'], [2, 'b'], [3, 'c']] -> Option D
Quick Check:
Zip pairs elements by index = B [OK]
Hint: Zip pairs elements index-wise into nested arrays [OK]
Common Mistakes:
Expecting flat array instead of nested arrays
Confusing zip with concatenation
Misunderstanding nil insertion
4. The following Ruby code is intended to zip two arrays but raises an error. What is the problem?
a = [1, 2]
b = ["x", "y", "z"]
result = a.zip *b
puts result.inspect
medium
A. Using double quotes instead of single quotes causes error.
B. Arrays have different lengths, causing an error.
C. Missing parentheses around argument in zip *b call.
D. The variable result is not defined before use.
Solution
Step 1: Check method call syntax
In Ruby, calling zip without parentheses is allowed only if no ambiguity exists, but here parentheses are recommended.
Step 2: Confirm array length and variable usage
Different lengths do not cause errors; zip fills missing with nil. Variables are defined correctly.
Final Answer:
Missing parentheses around argument in zip *b call. -> Option C
Quick Check:
Use parentheses for method arguments to avoid errors = C [OK]
Hint: Use parentheses with method arguments to avoid syntax errors [OK]
Common Mistakes:
Assuming different lengths cause errors
Ignoring parentheses in method calls
Confusing string quote types with errors
5. Given two arrays names = ["Alice", "Bob", "Carol"] and scores = [85, 92], which Ruby code correctly zips them and replaces missing scores with 0?