Bird
0
0

Which of the following is the correct syntax to use map with a block to add 3 to each element in array [1, 2, 3]?

easy📝 Syntax Q3 of 15
Ruby - Enumerable and Collection Processing
Which of the following is the correct syntax to use map with a block to add 3 to each element in array [1, 2, 3]?
A[1, 2, 3].map(x) -> x + 3
B[1, 2, 3].map { |x| x + 3 }
C[1, 2, 3].map do x + 3 end
D[1, 2, 3].map { x + 3 }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct block syntax for map

    The block must take a parameter with vertical bars: { |x| ... }.

  2. Step 2: Check each option's syntax

    [1, 2, 3].map { |x| x + 3 } uses correct block syntax. Others have syntax errors or missing block parameter.

  3. Final Answer:

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

    map block syntax with |var| [OK]
Quick Trick: Use {|var| ...} for map blocks in Ruby [OK]
Common Mistakes:
  • Omitting block parameter bars | |
  • Using incorrect block syntax like -> without do/end
  • Missing block parameter causing errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes