Bird
Raised Fist0
Rubyprogramming~20 mins

Map/collect for transformation in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using map?
Consider the following Ruby code that uses map to transform an array of numbers. What will be printed?
Ruby
numbers = [1, 2, 3, 4]
result = numbers.map { |n| n * 3 }
puts result.inspect
A[3, 6, 9, 12]
B[1, 2, 3, 4]
C[1, 4, 9, 16]
Dnil
Attempts:
2 left
💡 Hint
Remember, map creates a new array by applying the block to each element.
❓ Predict Output
intermediate
2:00remaining
What does this code output when using collect with a condition?
Look at this Ruby code using collect with a conditional inside the block. What is the output?
Ruby
arr = [5, 10, 15, 20]
result = arr.collect { |x| x > 10 ? x / 5 : x }
puts result.inspect
A[1, 2, 3, 4]
B[5, 10, 3, 4]
C[5, 10, 15, 20]
D[5, 2, 3, 4]
Attempts:
2 left
💡 Hint
Check how the ternary operator changes values greater than 10.
🔧 Debug
advanced
2:00remaining
Why does this map code raise an error?
This Ruby code tries to use map to transform an array but raises an error. What is the cause?
Ruby
arr = [1, 2, 3]
result = arr.map do |x|
x * 2
end
result = result + 1
puts result.inspect
ANoMethodError: undefined method `+' for nil:NilClass
BSyntaxError: unexpected end-of-input
CTypeError: no implicit conversion of Integer into Array
DNo error, outputs [3, 4, 5]
Attempts:
2 left
💡 Hint
Look at the line where result is added to 1.
❓ Predict Output
advanced
2:00remaining
What is the output of this nested map transformation?
Given this Ruby code with nested arrays and map, what will be printed?
Ruby
matrix = [[1, 2], [3, 4]]
result = matrix.map { |row| row.map { |n| n + 1 } }
puts result.inspect
A[[2, 3], [4, 5]]
B[[1, 2], [3, 4]]
C[[3, 4], [5, 6]]
D[2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Each inner array is transformed by adding 1 to each element.
🧠 Conceptual
expert
2:00remaining
How many elements are in the resulting array after this map?
What is the number of elements in the array returned by this Ruby code?
Ruby
arr = [1, 2, 3, 4, 5]
result = arr.map { |x| x if x.even? }
puts result.compact.size
A3
B0
C5
D2
Attempts:
2 left
💡 Hint
Remember that map keeps the same number of elements, but some may be nil.

Practice

(1/5)
1. What does the Ruby method map (or collect) do to an array?
easy
A. It sorts the array in ascending order.
B. It deletes items from the original array.
C. It creates a new array by changing each item based on the block given.
D. It returns the first item of the array.

Solution

  1. Step 1: Understand the purpose of map

    The map method takes each item in the array and applies the block's code to it.
  2. Step 2: Result of map

    This creates a new array with the changed items, leaving the original array unchanged.
  3. Final Answer:

    It creates a new array by changing each item based on the block given. -> Option C
  4. Quick Check:

    map changes items and returns new array [OK]
Hint: Remember: map returns a new changed array, original stays same [OK]
Common Mistakes:
  • Thinking map changes the original array
  • Confusing map with delete or sort methods
  • Believing map returns a single value
2. Which of the following is the correct syntax to double each number in the array [1, 2, 3] using map?
easy
A. [1, 2, 3].map { |x| x * 2 }
B. [1, 2, 3].map(x => x * 2)
C. [1, 2, 3].map do x x * 2 end
D. [1, 2, 3].map { x -> x * 2 }

Solution

  1. Step 1: Identify correct block syntax

    In Ruby, map uses a block with { |variable| code } or do |variable| code end.
  2. Step 2: Check each option

    [1, 2, 3].map { |x| x * 2 } uses correct block syntax with { |x| x * 2 }. Others have syntax errors or wrong block format.
  3. Final Answer:

    [1, 2, 3].map { |x| x * 2 } -> Option A
  4. Quick Check:

    Use curly braces and pipe for block variables [OK]
Hint: Use { |var| ... } for map blocks in one line [OK]
Common Mistakes:
  • Missing pipes around block variable
  • Using arrow syntax like JavaScript
  • Incorrect do-end block without pipes
3. What is the output of this Ruby code?
[4, 5, 6].map { |n| n + 1 }
medium
A. [4, 5, 6]
B. Error: undefined method
C. [3, 4, 5]
D. [5, 6, 7]

Solution

  1. Step 1: Understand the block operation

    The block adds 1 to each number in the array: 4+1=5, 5+1=6, 6+1=7.
  2. Step 2: Result of map

    The map method returns a new array with these values: [5, 6, 7].
  3. Final Answer:

    [5, 6, 7] -> Option D
  4. Quick Check:

    Each item +1 = [5, 6, 7] [OK]
Hint: Add inside block to transform each item [OK]
Common Mistakes:
  • Expecting original array to change
  • Confusing map with each (which returns original array)
  • Thinking map returns a single value
4. Find the error in this code snippet:
numbers = [1, 2, 3]
numbers.map { n n * 2 }
medium
A. The block should use parentheses
B. Missing pipe symbols around block variable
C. map cannot be used on arrays
D. The array should be a hash

Solution

  1. Step 1: Check block syntax

    The block is missing pipes around the variable: it should be { |n| n * 2 }.
  2. Step 2: Understand Ruby block rules

    Without pipes, Ruby does not know the block variable, causing a syntax error.
  3. Final Answer:

    Missing pipe symbols around block variable -> Option B
  4. Quick Check:

    Always use |var| in map blocks [OK]
Hint: Always put pipes | | around block variables [OK]
Common Mistakes:
  • Omitting pipes in block parameters
  • Thinking map is invalid on arrays
  • Trying to use parentheses instead of pipes
5. Given an array of strings ["apple", "banana", "cherry"], how do you use map to create a new array with the length of each string?
hard
A. ["apple", "banana", "cherry"].map { |fruit| fruit.length }
B. ["apple", "banana", "cherry"].map { |fruit| fruit.upcase }
C. ["apple", "banana", "cherry"].map { |fruit| fruit[0] }
D. ["apple", "banana", "cherry"].map { |fruit| fruit.reverse }

Solution

  1. Step 1: Identify the desired transformation

    We want the length of each string, so we use fruit.length inside the block.
  2. Step 2: Apply map with correct block

    Using map { |fruit| fruit.length } returns a new array of lengths.
  3. Final Answer:

    ["apple", "banana", "cherry"].map { |fruit| fruit.length } -> Option A
  4. Quick Check:

    Map each string to its length [OK]
Hint: Use .length inside map block to get string sizes [OK]
Common Mistakes:
  • Using unrelated methods like upcase or reverse
  • Trying to get length without block variable
  • Confusing map with each for transformation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard