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
36: Building a Text Processing Tool
How do I read a massive text file without crashing my program?
I've seen a lot of developers start by using File.read("data.txt"). That works fine when you're dealing with a few kilobytes, but the moment you point your tool at a 2GB production log file, your RAM is going to vanish and your OS will probably kill the process. You don't want to load the whole file into memory at once.
The trick is to stream the file line-by-line. In Ruby, File.foreach is your best friend here. It opens the file, yields each line to a block, and then closes the file automatically when it's done. It's memory-efficient because it only ever holds one line in memory at a time.
# Instead of File.read, do this:
File.foreach("production.log") do |line|
# Process each line individually
puts "Processing: #{line}" if line.include?("CRITICAL")
end
What's the cleanest way to extract specific data from a messy string?
When you're building a processing tool, you're usually looking for a needle in a haystack. You could use split or slice, but once the text format gets slightly inconsistent, those methods break. Regular Expressions (Regex) are the way to go, but I highly recommend using named captures. They make your code much more readable because you aren't guessing what match[1] or match[2] refers to.
Let's say we're parsing a log that looks like [2023-10-01] ERROR: Database timeout. Here is how I'd handle that:
log_line = "[2023-10-01] ERROR: Database timeout"
pattern = /\[(?<date>.*?)\] (?<level>.*?): (?<message>.*)/
if (match = log_line.match(pattern))
puts "Date: #{match[:date]}" # 2023-10-01
puts "Level: #{match[:level]}" # ERROR
puts "Message: #{match[:message]}" # Database timeout
end
It takes a second to get the syntax right, but it saves you from the "magic number" headache later on. Just a heads up: Regex can become a rabbit hole. If your pattern is getting too long to read, break it up or comment it heavily.
Should I just write a long script or wrap this in a class?
If this is a one-off script you'll run once and delete, a script is fine. But if you're building a "tool," you want it to be testable and reusable. I always suggest wrapping the logic in a class. This allows you to maintain state—like a counter for how many errors you've found—without relying on global variables, which are generally a bad idea in Ruby.
Here is a skeletal structure of how I'd organize a text processor. Notice how I separate the reading of the file from the processing of the data. This makes it way easier to write a test for the logic without actually needing a physical file on disk.
class LogSummarizer
attr_reader :stats
def initialize
@stats = Hash.new(0)
end
def process_file(path)
File.foreach(path) do |line|
process_line(line)
end
end
def process_line(line)
# We only care about the log level (INFO, WARN, ERROR)
if line =~ / (INFO|WARN|ERROR): /
level = $1
@stats[level] += 1
end
end
end
summarizer = LogSummarizer.new
summarizer.process_file("server.log")
puts summarizer.stats # {"INFO" => 120, "WARN" => 15, "ERROR" => 3}📋 Practical Task
Exercise: The Server Error Frequency Analyzer
Your task is to build a tool that analyzes a system log and identifies which specific error messages are occurring most frequently.
Requirements:
- Create a class called
ErrorAnalyzer. - Implement a method that reads a file line-by-line using
File.foreach. - Use a regular expression to extract only the message part of lines that start with
"ERROR:"(ignore INFO or WARN lines). - Store the count of each unique error message in a hash.
- Create a method called
top_errorsthat returns the top 3 most frequent error messages and their counts.
Test Data: Create a dummy file named system.log with at least 10 lines, ensuring some errors are repeated (e.g., ERROR: Connection Timeout appearing three times) so you can verify your sorting logic works.
There are no comments for now.