Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Ruby - Blocks, Procs, and Lambdas
Consider this code:
def transform(arr)
  arr.map { |x| yield x * 2 }
end
result = transform([1, 2, 3]) { |n| n + 1 }
puts result.inspect

What is the output?
A[1, 2, 3]
B[2, 4, 6]
C[3, 5, 7]
DError: no block given
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method behavior

    Method doubles each element, then yields to block which adds 1.
  2. Step 2: Calculate each transformed element

    For 1: 1*2=2, block adds 1 -> 3; for 2: 4+1=5; for 3: 6+1=7.
  3. Final Answer:

    [3, 5, 7] -> Option C
  4. Quick Check:

    Block modifies doubled values [OK]
Quick Trick: Block transforms each doubled element [OK]
Common Mistakes:
  • Ignoring block addition
  • Confusing map with each
  • Expecting original array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes