Bird
0
0

Identify the error in this code:

medium📝 Debug Q6 of 15
Ruby - Functional Patterns in Ruby
Identify the error in this code:
lazy_enum = (1..5).lazy.select { |x| x.even? }.map { |x| x * 3 }
puts lazy_enum[2]
ALazy enumerators do not support [] indexing
BThe block syntax is incorrect
CRange (1..5) cannot be lazy
DMissing call to .force before indexing
Step-by-Step Solution
Solution:
  1. Step 1: Understand lazy enumerator capabilities

    Lazy enumerators do not support direct indexing with [].
  2. Step 2: Check other options

    Block syntax is correct; ranges can be lazy; .force is not needed but .to_a or .force can be used to convert before indexing.
  3. Final Answer:

    Lazy enumerators do not support [] indexing -> Option A
  4. Quick Check:

    Lazy enumerators lack [] method [OK]
Quick Trick: Use .to_a or .force before indexing lazy enumerators [OK]
Common Mistakes:
  • Trying to index lazy enumerators directly
  • Assuming lazy enumerators are arrays
  • Confusing lazy with immediate enumerators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes