Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to combine two arrays element-wise using zip.
Ruby
a = [1, 2, 3] b = [4, 5, 6] combined = a.[1](b) puts combined.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of zip will not pair elements from both arrays.
✗ Incorrect
The zip method combines elements from two arrays into pairs.
2fill in blank
mediumComplete the code to print the first pair from the zipped arrays.
Ruby
a = ['a', 'b', 'c'] b = [1, 2, 3] zipped = a.zip(b) puts zipped[[1]].inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as index will give the second pair, not the first.
✗ Incorrect
Array indices start at 0, so the first pair is at index 0.
3fill in blank
hardFix the error in the code to correctly zip arrays and print the result.
Ruby
x = [10, 20] y = [30, 40] result = x.[1](y) puts result.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses or exclamation mark causes errors.
✗ Incorrect
The correct method name is zip without exclamation or parentheses.
4fill in blank
hardFill both blanks to create a hash from two arrays using zip and to_h.
Ruby
keys = [:name, :age, :city] values = ['Alice', 30, 'NYC'] hash = keys.[1](values).[2] puts hash.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of zip or missing to_h conversion.
✗ Incorrect
Use zip to pair keys and values, then to_h to convert pairs to a hash.
5fill in blank
hardFill all three blanks to zip three arrays and print the combined array.
Ruby
a = [1, 2] b = ['x', 'y'] c = [:alpha, :beta] combined = a.[1](b).[2] { |pair| pair.[3](:zip, c) } puts combined.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using each instead of map or wrong method names.
✗ Incorrect
First zip a and b, then map each pair to call send(:zip) on it to combine with c.