Bird
0
0

What will this code output?

medium📝 Predict Output Q5 of 15
Ruby - Functional Patterns in Ruby
What will this code output?
enum = (1..10).lazy.map { |x| x * 2 }.select { |x| x > 10 }.force
p enum
A[2, 4, 6, 8, 10]
B[11, 12, 13, 14, 15]
C[12, 14, 16, 18, 20]
DError: No method 'force' for Enumerator
Step-by-Step Solution
Solution:
  1. Step 1: Trace lazy map and select

    Map doubles numbers 1 to 10: [2,4,6,8,10,12,14,16,18,20]. Select keeps those > 10: [12,14,16,18,20].
  2. Step 2: Understand .force usage

    .force converts lazy enumerator to an array, so enum is the filtered array.
  3. Final Answer:

    [12, 14, 16, 18, 20] -> Option C
  4. Quick Check:

    Lazy map and select then force = [12, 14, 16, 18, 20] [OK]
Quick Trick: Use .force to realize lazy enumerator into array [OK]
Common Mistakes:
  • Confusing .force with .to_a
  • Selecting wrong elements after map
  • Assuming .force is undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes