Bird
0
0

What will this Ruby method return?

hard📝 Application Q9 of 15
Ruby - Methods
What will this Ruby method return?
def nested
  x = 3
  y = (1..5).map do |n|
    n * x
  end
  y.select { |v| v > 10 }
end
result = nested
A[3, 6, 9, 12, 15]
B[1, 2, 3]
Cnil
D[12, 15]
Step-by-Step Solution
Solution:
  1. Step 1: Calculate map block

    Mapping 1..5 with n * x where x=3 gives [3, 6, 9, 12, 15].
  2. Step 2: Apply select block

    Select values greater than 10: [12, 15]. This is the last expression.
  3. Final Answer:

    [12, 15] -> Option D
  4. Quick Check:

    Implicit return returns last expression value [OK]
Quick Trick: Last expression in method is returned implicitly [OK]
Common Mistakes:
  • Returning mapped array instead of filtered
  • Ignoring select filtering
  • Confusing variable scopes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes