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
11: Yield and Enumerators
I've always found that the most confusing part of Ruby isn't the syntax—it's the "invisible" passing of blocks. You see each or map everywhere, but if you've ever tried to write your own method that behaves like those, you probably hit a wall pretty quickly. Let's figure out how that actually works by trying to build a simple "batch processor."
The "Magic" Pause Button
Imagine we have a list of heavy tasks, and we want a method that handles the setup and teardown—like opening a connection to a database—and then lets us decide what to do with each item. I'll start with something simple: a method that just yields.
def process_batch(items)
puts "--- Starting Batch ---"
items.each do |item|
yield(item)
end
puts "--- Batch Finished ---"
end
process_batch(["email_1", "email_2", "email_3"]) do |email|
puts "Sending #{email}..."
end
When I run this, it works exactly as expected. The yield keyword is essentially a "pause" button. Ruby jumps out of the process_batch method, runs the code inside the block, and then jumps back right where it left off. It's a powerful way to separate the plumbing (the start/stop logs) from the logic (sending the email).
The Crash when the Block is Missing
But here's the problem. I'm a human, and eventually, I'm going to forget to pass a block. Let's see what happens if I just call the method on its own.
process_batch(["email_1", "email_2"])
# LocalVariableError: undefined method `yield' for ...
# Actually, it's a LocalJumpError: no block given
Ruby doesn't just ignore the yield; it throws a LocalJumpError because it has nowhere to jump to. In a real production app, that's a crash. To fix this, I need to check if a block was even provided. I'll use block_given?.
def process_batch(items)
puts "--- Starting Batch ---"
if block_given?
items.each { |item| yield(item) }
else
puts "No block provided, nothing to do!"
end
puts "--- Batch Finished ---"
end
That's safer, but it's a bit boring. If I don't provide a block, I just get a message. I'd much rather have the method return something that I can use later. This is where things get interesting.
Turning a Method into a Stream
If you've used Range#each, you know you can do things like (1..10).each.with_index. That .each call is returning an Enumerator. I want my process_batch to do the same. Instead of just printing a warning, I'll use enum_for.
def process_batch(items)
return enum_for(:process_batch, items) unless block_given?
puts "--- Starting Batch ---"
items.each { |item| yield(item) }
puts "--- Batch Finished ---"
end
# Now, I can do this:
batch = process_batch(["email_1", "email_2", "email_3"])
puts "I have the enumerator, but the batch hasn't started yet!"
# Now I can use any Enumerable method on it:
batch.with_index { |email, idx| puts "#{idx}: #{email}" }
Notice what happened. When I called process_batch without a block, it didn't execute the "Starting Batch" print statement. It just handed me an Enumerator object. The actual execution was deferred until I called .with_index. This is a professional pattern: provide the convenience of a block for simple cases, but return an Enumerator for those who want to chain methods or handle the iteration lazily.
By returning enum_for(:method_name, args), you've essentially told Ruby: "If the user didn't give me a block now, create a package that remembers this method and these arguments, and let the user trigger the yield whenever they're ready."
📋 Practical Task
Build a Log File Line-Filter
You are building a tool to analyze large log files. Instead of loading the whole file into memory, you need to create a method that yields lines one by one. Your goal is to implement a method called filter_logs(filename, keyword).
Requirements:
- The method should open the file and yield only the lines that contain the specified
keyword. - If a block is provided, it should execute immediately (yielding the matching lines).
- If no block is provided, it must return an
Enumeratorso that the caller can chain methods like.first(5)or.map. - Ensure the file is closed properly (use a block for
File.open).
Test your code with these two scenarios:
# Scenario 1: Immediate execution
filter_logs("production.log", "ERROR") { |line| puts "Found error: #{line}" }
# Scenario 2: Enumerator chaining
errors = filter_logs("production.log", "ERROR")
puts "The first 3 errors are: #{errors.first(3)}"
There are no comments for now.