Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
47: The Enumerable Module in Depth
I've noticed a recurring pattern when developers start building their own collection classes in Ruby: they assume that Enumerable is a "magic" module that automatically knows how to traverse any object it's mixed into. They'll include Enumerable at the top of their class, try to call .map or .select, and then stare in confusion when Ruby throws a NoMethodError for each.
The "Include and it Works" Fallacy
The misconception is that Enumerable provides the logic for how to iterate. It doesn't. It provides a massive library of high-level tools (like find_all, group_by, and reduce) that all rely on a single, specific contract: the existence of an each method.
# This is what the misconception looks like in code
class Playlist
include Enumerable
def initialize(songs)
@songs = songs # assume this is an array
end
end
my_list = Playlist.new(["Song A", "Song B"])
my_list.map { |s| s.upcase }
# NoMethodError (undefined method `each' for #<Playlist:0x000...>)
As you can see, Enumerable is essentially a set of plugins. But these plugins are useless if there's no engine to drive them. In this case, each is the engine.
Enumerable as a Mixin Powered by each
To make Enumerable work, you have to implement the each method yourself. This method is the "source of truth" for how your object should be stepped through. Once each exists, Enumerable suddenly "unlocks" dozens of other methods because it simply calls your each method internally to do its work.
Let's fix the Playlist example. I'll define each so that it yields every song in the internal array to the block provided.
class Playlist
include Enumerable
def initialize(songs)
@songs = songs
end
def each
@songs.each do |song|
yield song
end
end
end
my_list = Playlist.new(["Song A", "Song B", "Song C"])
# Now this works! .select is provided by Enumerable, but it uses our .each
rock_songs = my_list.select { |s| s.include?("Rock") }
I want you to think of Enumerable as a layer of abstraction. You define the primitive (the each method), and Ruby gives you the conveniences (everything else). This is a beautiful example of the "Composition over Inheritance" philosophy in Ruby.
The Pro Move: Returning an Enumerator
If you look at the source code for Ruby's core Array class, you'll notice that each doesn't just yield values—it can also return an Enumerator object if no block is given. If you want your custom collection to feel like a first-class Ruby citizen, you should mimic this behavior.
By adding a simple check for the block, you allow your users to chain methods or call .with_index on your collection, which is an incredibly powerful pattern.
class Playlist
include Enumerable
def initialize(songs)
@songs = songs
end
def each
# If the user didn't provide a block, return an Enumerator
return to_enum(:each) unless block_given?
@songs.each { |song| yield song }
end
end
my_list = Playlist.new(["Song A", "Song B", "Song C"])
# Because we returned an Enumerator, we can do this:
my_list.each.with_index { |song, i| puts "#{i + 1}. #{song}" }
Adding return to_enum(:each) unless block_given? might seem like a pedantic detail, but it's the difference between a script that "just works" and a professional library that integrates seamlessly with the rest of the Ruby ecosystem.
📋 Practical Task
Build a TransactionHistory Filter
You are building a financial ledger system. Create a class called TransactionHistory that stores a list of transaction hashes (each hash should have an :amount and a :category).
Your task is to:
- Include the
Enumerablemodule. - Implement the
eachmethod so that the class is enumerable (ensure it handles the case where no block is given by returning an enumerator). - Using the power of
Enumerable(without writing new methods inside the class), perform the following operations on an instance ofTransactionHistory:- Find the total sum of all transactions using
reduce(orsum). - Filter the transactions to find only those in the "Food" category using
select. - Find the first transaction that exceeds $100 using
find.
- Find the total sum of all transactions using
There are no comments for now.