Bird
0
0

Given this Ruby method, what will it return?

hard📝 Application Q8 of 15
Ruby - Methods
Given this Ruby method, what will it return?
def complex
  arr = [1, 2, 3]
  arr.map { |n| n * 2 }
  arr
end
result = complex
Anil
B[1, 2, 3]
C[2, 4, 6]
DError
Step-by-Step Solution
Solution:
  1. Step 1: Trace method statements

    arr.map { |n| n * 2 } creates a new array but does not modify arr. The last expression is arr.
  2. Step 2: Understand implicit return

    The method returns the last expression arr, which is still [1, 2, 3].
  3. Final Answer:

    [1, 2, 3] -> Option B
  4. Quick Check:

    Implicit return returns last expression value [OK]
Quick Trick: map returns new array; last expression is original array [OK]
Common Mistakes:
  • Assuming arr is changed by map
  • Thinking method returns mapped array
  • Ignoring implicit return of last expression

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes