Bird
Raised Fist0
Rubyprogramming~20 mins

Why Enumerable is Ruby's most powerful module - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Enumerable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Enumerable#map with a block
What is the output of this Ruby code using Enumerable's map method?
Ruby
numbers = [1, 2, 3, 4]
result = numbers.map { |n| n * 2 }
puts result.inspect
A[2, 4, 6, 8]
B[1, 2, 3, 4]
C8
Dnil
Attempts:
2 left
💡 Hint
Enumerable#map applies the block to each element and returns a new array.
🧠 Conceptual
intermediate
1:30remaining
Why does Enumerable require #each?
Why does the Enumerable module require the including class to define the #each method?
ABecause Enumerable methods use #each to iterate over elements.
BBecause #each is used to sort elements automatically.
CBecause #each defines the class constructor.
DBecause #each is needed to convert elements to strings.
Attempts:
2 left
💡 Hint
Enumerable methods rely on a way to visit each element in a collection.
🔧 Debug
advanced
2:30remaining
Fix the error when using Enumerable methods
What error will this code produce and why? class Box include Enumerable def initialize(items) @items = items end end box = Box.new([1, 2, 3]) box.map { |x| x * 2 }
ATypeError: can't convert Box to Array
BArgumentError: wrong number of arguments for map
CNoMethodError: undefined method `each' for #<Box:...>
DSyntaxError: unexpected end-of-input
Attempts:
2 left
💡 Hint
Enumerable needs #each to work properly.
🚀 Application
advanced
3:00remaining
Implement #each to use Enumerable methods
Complete the Box class so that Enumerable methods like #select work correctly on its items.
Ruby
class Box
  include Enumerable
  def initialize(items)
    @items = items
  end

  # Your code here
end

box = Box.new([1, 2, 3, 4])
selected = box.select { |x| x.even? }
puts selected.inspect
A
def each
  yield @items
end
B
def each
  @items.each
end
C
def each
  @items.map { |item| yield item }
end
D
def each
  @items.each { |item| yield item }
end
Attempts:
2 left
💡 Hint
The #each method must yield each element to the block.
❓ Predict Output
expert
2:00remaining
Output of Enumerable#reduce with initial value
What is the output of this Ruby code using Enumerable's reduce method with an initial value?
Ruby
values = [2, 3, 4]
result = values.reduce(10) { |sum, n| sum + n }
puts result
A9
B19
C24
DTypeError
Attempts:
2 left
💡 Hint
Reduce starts with the initial value and adds each element.

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

  1. Step 1: Understand what Enumerable requires

    The module requires you to define only the each method in your class.
  2. Step 2: Recognize the benefit of defining each

    Once each is defined, you get many collection methods like map, select, and reduce for free.
  3. Final Answer:

    It provides many useful methods once you define each. -> Option D
  4. 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

  1. Step 1: Recall how modules are added to classes

    To add instance methods from a module, use include.
  2. Step 2: Check the syntax for including Enumerable

    The correct syntax is include Enumerable inside the class, and define each method.
  3. Final Answer:

    class MyCollection; include Enumerable; def each; end; end -> Option A
  4. 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

  1. Step 1: Understand the class and Enumerable inclusion

    The class defines each to yield elements from @nums, so Enumerable methods like map work.
  2. Step 2: Trace the map method call

    nums.map { |x| x * 2 } doubles each element: 1*2=2, 2*2=4, 3*2=6.
  3. Final Answer:

    [2, 4, 6] -> Option C
  4. 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

  1. 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.
  2. Step 2: Understand why this causes an error

    Enumerable methods expect each to yield elements; without yield, methods like select won't work properly.
  3. Final Answer:

    The each method does not yield elements to the block. -> Option B
  4. 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

  1. Step 1: Ensure each yields elements from the collection

    Implement the each method to yield each element so Enumerable methods work.
  2. Step 2: Use Enumerable methods to filter and sum

    Call select(&:even?) to get even numbers, then sum to add them.
  3. Final Answer:

    Define each to yield elements, then use select(&:even?).sum on the instance. -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard