0
0
Rubyprogramming~10 mins

Lazy enumerators in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a lazy enumerator from an array.

Ruby
numbers = [1, 2, 3, 4, 5]
lazy_enum = numbers.[1]
Drag options to blanks, or click blank then click option'
Alazy
Bmap
Cselect
Deach
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' or 'select' directly without 'lazy' creates immediate enumerables.
Using 'each' returns the original enumerable, not lazy.
2fill in blank
medium

Complete the code to get the first 3 even numbers lazily.

Ruby
numbers = (1..Float::INFINITY).lazy
result = numbers.select { |n| n.even? }.[1](3).force
Drag options to blanks, or click blank then click option'
Atake
Bmap
Cdrop
Dfirst
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'first' returns an element immediately, not a lazy enumerator.
Using 'drop' skips elements instead of taking them.
3fill in blank
hard

Fix the error in the code to lazily map and select odd numbers.

Ruby
numbers = (1..10).lazy
result = numbers.[1] { |n| n * 2 }.select(&:odd?).force
Drag options to blanks, or click blank then click option'
Aselect
Beach
Cmap
Dreject
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'select' instead of 'map' applies a filter, not a transformation.
Using 'each' does not return a lazy enumerator.
4fill in blank
hard

Fill both blanks to create a lazy enumerator that drops the first 5 elements and takes the next 3.

Ruby
numbers = (1..100).lazy
result = numbers.[1](5).[2](3).force
Drag options to blanks, or click blank then click option'
Adrop
Btake
Cselect
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Reversing the order of 'take' and 'drop' changes the result.
Using 'select' or 'map' instead of 'drop' or 'take' changes behavior.
5fill in blank
hard

Fill all three blanks to create a lazy enumerator that maps numbers to their squares and selects those greater than 10.

Ruby
numbers = (1..10).lazy
result = numbers.[1] { |n| n * n }.[2] { |n| n [3] 10 }.force
Drag options to blanks, or click blank then click option'
Amap
Bselect
C>
Ddrop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'drop' instead of 'select' changes filtering behavior.
Using '<' instead of '>' changes the filter condition.