What is the main purpose of the Enumerable module in Ruby?
Enumerable provides a set of methods to traverse, search, sort, and manipulate collections like arrays and hashes in a consistent way.
Click to reveal answer
beginner
How does a class gain access to Enumerable's powerful methods?
By including the Enumerable module and defining an <code>each</code> method, a class automatically gets access to all Enumerable methods.
Click to reveal answer
intermediate
Why is defining each method crucial for Enumerable?
Enumerable relies on each to iterate over elements. Once each is defined, Enumerable can build many useful methods on top of it.
Click to reveal answer
beginner
Name three common methods provided by Enumerable.
Some common Enumerable methods are map, select, and reduce (also known as inject).
Click to reveal answer
intermediate
Why is Enumerable considered powerful compared to writing custom iteration methods?
Enumerable saves time and effort by providing many ready-to-use methods that work on any collection with each, promoting code reuse and consistency.
Click to reveal answer
What must a class implement to use Enumerable methods?
AA <code>map</code> method
BAn <code>each</code> method
CA <code>select</code> method
DA <code>reduce</code> method
✗ Incorrect
Enumerable depends on the each method to iterate over elements.
Which of these is NOT a method provided by Enumerable?
A<code>push</code>
B<code>select</code>
C<code>map</code>
D<code>reduce</code>
✗ Incorrect
push is a method for arrays, not part of Enumerable.
Why is Enumerable called powerful?
AIt provides many methods after defining only <code>each</code>
BIt automatically creates classes
CIt replaces Ruby's core classes
DIt only works with arrays
✗ Incorrect
Enumerable builds many useful methods on top of the each method.
Which module do you include to get Enumerable methods?
AEnumerableModule
BEnumerableMethods
CEnumerable
DEnumerableClass
✗ Incorrect
The module is named exactly Enumerable.
What kind of collections can use Enumerable?
AOnly strings
BOnly arrays
COnly hashes
DAny collection with an <code>each</code> method
✗ Incorrect
Enumerable works with any collection that defines each.
Explain why defining the each method is essential for using Enumerable in Ruby.
Think about how Enumerable builds its methods on iteration.
You got /3 concepts.
Describe three benefits of using the Enumerable module in your Ruby programs.
Consider how Enumerable saves time and effort.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason Enumerable is considered Ruby's most powerful module?
easy
A. It speeds up your program by using threads.
B. It automatically creates classes for you.
C. It replaces the need for loops entirely.
D. It provides many useful methods once you define each.
Solution
Step 1: Understand what Enumerable requires
The module requires you to define only the each method in your class.
Step 2: Recognize the benefit of defining each
Once each is defined, you get many collection methods like map, select, and reduce for free.
Final Answer:
It provides many useful methods once you define each. -> Option D
Quick Check:
Defining each unlocks Enumerable methods = C [OK]
Hint: Remember: define each, get many methods [OK]
Common Mistakes:
Thinking Enumerable creates classes
Believing it manages threads automatically
Assuming it removes all loops
2. Which of the following is the correct way to include Enumerable in a Ruby class?
easy
A. class MyCollection; include Enumerable; def each; end; end
B. class MyCollection; extend Enumerable; def each; end; end
C. class MyCollection; require Enumerable; def each; end; end
D. class MyCollection; import Enumerable; def each; end; end
Solution
Step 1: Recall how modules are added to classes
To add instance methods from a module, use include.
Step 2: Check the syntax for including Enumerable
The correct syntax is include Enumerable inside the class, and define each method.
Final Answer:
class MyCollection; include Enumerable; def each; end; end -> Option A
Quick Check:
Use include for instance methods = A [OK]
Hint: Use include to add Enumerable instance methods [OK]
Common Mistakes:
Using extend instead of include
Trying to require or import modules
Not defining the each method
3. What will be the output of this Ruby code?
class Numbers
include Enumerable
def initialize(nums)
@nums = nums
end
def each
@nums.each { |n| yield n }
end
end
nums = Numbers.new([1, 2, 3])
p nums.map { |x| x * 2 }
medium
A. [1, 2, 3]
B. NoMethodError
C. [2, 4, 6]
D. SyntaxError
Solution
Step 1: Understand the class and Enumerable inclusion
The class defines each to yield elements from @nums, so Enumerable methods like map work.
Step 2: Trace the map method call
nums.map { |x| x * 2 } doubles each element: 1*2=2, 2*2=4, 3*2=6.
Final Answer:
[2, 4, 6] -> Option C
Quick Check:
Enumerable map doubles elements = [2, 4, 6] [OK]
Hint: Enumerable methods work if each yields elements [OK]
Common Mistakes:
Forgetting to define each method
Confusing map output with original array
Expecting errors without yield
4. Identify the error in this Ruby class using Enumerable:
class MyList
include Enumerable
def initialize(items)
@items = items
end
def each
@items.each
end
end
list = MyList.new([1, 2, 3])
p list.select { |x| x > 1 }
medium
A. The initialize method is missing.
B. The each method does not yield elements to the block.
C. select method is not defined in Enumerable.
D. Enumerable cannot be included in custom classes.
Solution
Step 1: Check the each method implementation
The each method calls @items.each but does not yield elements to the block given to MyList's each.
Step 2: Understand why this causes an error
Enumerable methods expect each to yield elements; without yield, methods like select won't work properly.
Final Answer:
The each method does not yield elements to the block. -> Option B
Quick Check:
each must yield elements = D [OK]
Hint: Ensure each yields elements to the block [OK]
Common Mistakes:
Assuming calling each without yield is enough
Thinking Enumerable can't be included
Believing select is not part of Enumerable
5. Given a custom class that includes Enumerable, how can you use Enumerable methods to find the sum of even numbers in a collection stored inside the class?
hard
A. Define each to yield elements, then use select(&:even?).sum on the instance.
B. Override the sum method to filter even numbers manually.
C. Use a for loop outside the class to sum even numbers.
D. Enumerable does not support sum or select methods.
Solution
Step 1: Ensure each yields elements from the collection
Implement the each method to yield each element so Enumerable methods work.
Step 2: Use Enumerable methods to filter and sum
Call select(&:even?) to get even numbers, then sum to add them.
Final Answer:
Define each to yield elements, then use select(&:even?).sum on the instance. -> Option A
Quick Check:
each + select + sum = A [OK]
Hint: Use select(&:even?).sum after defining each [OK]
Common Mistakes:
Trying to override sum unnecessarily
Using loops outside instead of Enumerable methods
Thinking Enumerable lacks sum or select
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.