Ruby - Functional Patterns in RubyHow can you combine lazy enumerators with infinite sequences to find the first 4 prime numbers greater than 50?A(1..50).lazy.select { |n| prime?(n) }.first(4)B(51..Float::INFINITY).lazy.select { |n| prime?(n) }.first(4)C(50..100).lazy.select { |n| prime?(n) }.first(4)D(51..Float::INFINITY).select { |n| prime?(n) }.first(4)Check Answer
Step-by-Step SolutionSolution:Step 1: Use infinite range starting after 50Range from 51 to infinity lazily filtered for primes.Step 2: Use lazy select and first(4)Lazy select filters primes without computing all; first(4) gets first 4 primes > 50.Step 3: Check other options(1..50).lazy.select { |n| prime?(n) }.first(4) searches below 50; C is limited range; D is not lazy and infinite select causes hang.Final Answer:(51..Float::INFINITY).lazy.select { |n| prime?(n) }.first(4) -> Option BQuick Check:Lazy infinite prime filter = (51..Float::INFINITY).lazy.select { |n| prime?(n) }.first(4) [OK]Quick Trick: Combine lazy with infinite range for filtered sequences [OK]Common Mistakes:Using finite ranges missing primes > 50Not using lazy on infinite rangeUsing non-lazy select on infinite range
Master "Functional Patterns in Ruby" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Advanced Metaprogramming - DSL building patterns - Quiz 3easy Concurrent Programming - GIL (Global Interpreter Lock) impact - Quiz 12easy Functional Patterns in Ruby - Pipeline operator concept - Quiz 11easy Gems and Bundler - Why gem management matters - Quiz 6medium Gems and Bundler - Gem versions and constraints - Quiz 1easy Metaprogramming Fundamentals - Respond_to_missing? convention - Quiz 12easy Metaprogramming Fundamentals - Send for calling methods dynamically - Quiz 5medium Regular Expressions - Common patterns and character classes - Quiz 12easy Regular Expressions - Match operator (=~) - Quiz 15hard Ruby Ecosystem and Best Practices - Rubocop for linting - Quiz 1easy