Bird
0
0

Which method chain correctly creates a lazy enumerator that selects even numbers from 1 to 10?

easy📝 Conceptual Q2 of 15
Ruby - Functional Patterns in Ruby
Which method chain correctly creates a lazy enumerator that selects even numbers from 1 to 10?
A(1..10).select(&:even?).lazy
B(1..10).lazy.select(&:even?)
C(1..10).lazy.map(&:even?)
D(1..10).map(&:even?).lazy
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct lazy usage

    Calling .lazy first creates a lazy enumerator, then .select(&:even?) filters even numbers lazily.
  2. Step 2: Analyze other options

    Options B and D call .lazy after filtering or mapping, which returns an Enumerator but not lazy from the start. (1..10).lazy.map(&:even?) maps to booleans, not selects even numbers.
  3. Final Answer:

    (1..10).lazy.select(&:even?) -> Option B
  4. Quick Check:

    Lazy enumerator with select = (1..10).lazy.select(&:even?) [OK]
Quick Trick: Call .lazy before filtering to keep laziness [OK]
Common Mistakes:
  • Calling .lazy after select/map loses laziness
  • Using map instead of select for filtering
  • Confusing order of method calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes