Mastering advanced enumerables and enumerators in Ruby is essential for efficient data manipulation. Learn about Custom Enumerables, Lazy Enumerators, and how to work with Infinite Collections for optimized performance.
Custom Enumerables (Questions 1-10)
Which of the following is the correct way to define a custom enumerable in Ruby? a) class MyClass; include Enumerable; end b) class MyClass; extend Enumerable; end c) class MyClass; include Enum; end d) class MyClass; extend Enum; end
What is required when implementing the Enumerable module in Ruby? a) Defining the each method b) Defining the map method c) Defining the select method d) Defining the collect method
Which of the following methods is available to a class after including the Enumerable module? a) each b) each_with_index c) map d) All of the above
How do you define the each method in a custom enumerable? a) Define it as a class method b) Define it as an instance method c) Define it as a class variable d) Define it as a module method
Which of the following Ruby methods can be used with custom enumerables? a) collect b) find c) any? d) All of the above
When you include the Enumerable module in a class, what is the primary purpose of the each method? a) To iterate over the collection b) To filter elements c) To modify elements d) To count elements
In a custom enumerable, what does the all? method do? a) Returns true if all elements are true b) Returns true if any element is true c) Returns false if all elements are false d) Returns false if any element is false
Which method in Enumerable returns the first element that satisfies a condition? a) find b) map c) select d) reject
How does the inject method work in a custom enumerable? a) It transforms elements into a new collection b) It combines elements to return a single value c) It filters elements based on a condition d) It iterates over the collection
Which of the following methods is NOT part of the Enumerable module in Ruby? a) each b) any? c) add d) collect
Lazy Enumerators (Questions 11-20)
What is a lazy enumerator in Ruby? a) An enumerator that processes all elements immediately b) An enumerator that processes elements only when required c) An enumerator that never processes any elements d) An enumerator that throws an error on large collections
Which method in Ruby is used to create a lazy enumerator? a) to_lazy b) lazy c) lazy_enum d) enumerate
Which of the following is an advantage of using lazy enumerators in Ruby? a) Increased memory usage b) Processing elements only when needed c) Decreased performance d) Immediate collection of all elements
How does the lazy method affect the behavior of enumerables in Ruby? a) It processes all elements immediately b) It allows operations to be chained without executing until required c) It skips all elements d) It sorts the collection
What happens when you call lazy on an array in Ruby? a) It converts the array into a lazy enumerable b) It creates a copy of the array c) It makes the array immutable d) It filters the array’s elements
Which of the following methods can be used with a lazy enumerator? a) map b) select c) take d) All of the above
What is the primary benefit of chaining methods like map, select, and take on a lazy enumerator? a) They all execute immediately and store results b) They allow for deferred evaluation c) They increase memory usage d) They prevent the use of multiple enumerators
Which of the following operations will trigger the evaluation of a lazy enumerator? a) each b) map c) to_a d) select
How does the take method behave in the context of a lazy enumerator? a) It takes a specified number of elements from the collection b) It skips a specified number of elements c) It sorts the collection d) It executes all methods on the enumerator
In which scenario would a lazy enumerator be most beneficial? a) When working with small datasets b) When memory consumption is a concern with large datasets c) When immediate processing of all elements is required d) When filtering a collection
Working with Infinite Collections (Questions 21-30)
How do you create an infinite collection in Ruby? a) Using the Range.new method b) Using the loop method c) Using the Enumerator.new method with an infinite block d) Using infinite method
Which method in Ruby can be used to create an infinite sequence of numbers? a) to_enum b) Enumerator.new c) infinite d) each
What does the following code return in Ruby?rubyCopy codeEnumerator.new { |y| y.yield 1; y.yield 2; y.yield 3 } a) A range of numbers b) An infinite enumerator c) An enumerator that returns 1, 2, 3 d) An error
How can you limit the number of elements from an infinite collection in Ruby? a) Using the take method b) Using the limit method c) Using the first method d) Using the map method
Which of the following is an example of an infinite collection in Ruby? a) (1..) b) 1..Float::INFINITY c) (1...infinite) d) Both a and b
Which of the following methods can be used to take elements from an infinite collection? a) first b) each c) take d) drop
In Ruby, how do you create a lazy infinite sequence of even numbers? a) Enumerator.new { |y| y.yield 2; y.yield 4; y.yield 6 } b) (2..Float::INFINITY).lazy.select(&:even?) c) Enumerable.new { |y| y.yield 2; y.yield 4; y.yield 6 } d) 2.upto(Float::INFINITY)
What is the purpose of the lazy method when working with infinite collections? a) It allows only the required elements to be evaluated b) It computes all elements immediately c) It limits the range of elements d) It creates a fixed-length collection
Which Ruby method can be used to produce a finite collection from an infinite collection? a) take b) first c) slice d) select
How can you generate an infinite collection of Fibonacci numbers in Ruby? a) Enumerator.new { |y| y.yield 1; y.yield 1; y.yield 2 } b) (1..).lazy.select { |x| x % 2 == 0 } c) Enumerator.new { |y| a, b = 0, 1; loop { y.yield a; a, b = b, a + b } } d) (1...infinite).lazy
Answer Key
QNo
Answer (Option with Text)
1
a) class MyClass; include Enumerable; end
2
a) Defining the each method
3
d) All of the above
4
b) Define it as an instance method
5
d) All of the above
6
a) To iterate over the collection
7
a) Returns true if all elements are true
8
a) find
9
b) It combines elements to return a single value
10
c) add
11
b) An enumerator that processes elements only when required
12
b) lazy
13
b) Processing elements only when needed
14
b) It allows operations to be chained without executing until required
15
a) It converts the array into a lazy enumerable
16
d) All of the above
17
b) They allow for deferred evaluation
18
c) to_a
19
a) It takes a specified number of elements from the collection
20
b) When memory consumption is a concern with large datasets
21
c) Using the Enumerator.new method with an infinite block
22
b) Enumerator.new
23
c) An enumerator that returns 1, 2, 3
24
a) Using the take method
25
d) Both a and b
26
c) take
27
b) (2..Float::INFINITY).lazy.select(&:even?)
28
a) It allows only the required elements to be evaluated