A text pipeline
The input
A pipeline step is a program that reads a file, does one job and prints a result another program can read. The input here is a small access log, one request per line, with an occasional line that is not a request. Save it as access.log:
2026-11-05 12:00:01 GET /api/users 200 12 2026-11-05 12:00:02 GET /api/users 500 31 2026-11-05 12:00:03 POST /api/orders 502 44 2026-11-05 12:00:04 GET /api/users 200 9 2026-11-05 12:00:05 GET /health 200 1 2026-11-05 12:00:06 broken line 2026-11-05 12:00:07 GET /api/orders 500 27
The job is the one from a real incident: which paths fail, and how often. A line that does not parse is counted separately instead of being silently dropped, because a parser that hides bad input hides the bug.
The script
File.foreach reads one line at a time, so the size of the log does not matter. The pattern is the validating regex from lesson 8.10; the hash counts the paths with a 5xx status:
pattern = /\A(?<time>\S+ \S+) (?<method>[A-Z]+) (?<path>\S+) (?<status>\d{3}) (?<ms>\d+)\z/ errors = Hash.new(0) total = 0 skipped = 0 File.foreach(ARGV.fetch(0)) do |line| total += 1 m = line.chomp.match(pattern) if m.nil? skipped += 1 next end next unless m[:status].start_with?("5") errors[m[:path]] += 1 end puts "lines: #{total}, skipped: #{skipped}" errors.sort_by { |path, count| [-count, path] }.each do |path, count| puts format("%-12s %d", path, count) end
Two details carry a pipeline: ARGV.fetch(0) makes a missing argument an error instead of reading standard input by accident, and sort_by with a two-element key makes the output stable even when two paths have the same count.
Running it
ruby filter.rb access.log
lines: 7, skipped: 1 /api/orders 2 /api/users 1
The summary line goes first, so a reader sees that one line was not counted. The two failing paths follow, most frequent first, which is the order an on-call engineer reads.
In a pipeline the same script is a stage whose output is captured to a file, and a CI job diffs that file against a known good one. The exit code is the other interface; a version that must fail the build when errors are present ends with exit 1 unless errors.empty?.
The shape of a pipeline step
- One input. A file argument, not a guess. Standard input is a separate mode when the tool needs it.
- One job. Filter, count or convert; not all three in one script.
- Count what you skip. A line that does not parse is a number in the report, not a silent next.
- Stable output. Sorted keys and fixed widths, so a diff between two runs means a real change.
What to watch for
line.chomp is required before
\z: the newline at the end of a line otherwise
fails the anchor. The alternative is chomp: true
on readlines, which foreach
does not take.
A hash with a default of zero is the counter; a hash of lists is for a
report that needs the lines themselves, and it holds every matched line in
memory. Pick the aggregate the report prints, not the data behind it.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
File.write("access.log", "2026-01-01 00:00:00 GET /a 500 1\n2026-01-01 00:00:01 GET /a 500 2\n2026-01-01 00:00:02 GET /b 200 3\nbroken\n") pattern = /\A(?<time>\S+ \S+) (?<method>[A-Z]+) (?<path>\S+) (?<status>\d{3}) (?<ms>\d+)\z/ errors = Hash.new(0) total = 0 skipped = 0 File.foreach("access.log") do |line| total += 1 m = line.chomp.match(pattern) if m.nil? skipped += 1 next end next unless m[:status].start_with?("5") errors[m[:path]] += 1 end puts "lines: #{total}, skipped: #{skipped}"Answer
lines: 4, skipped: 1
Three request lines and one broken line are read, and the broken line is counted as skipped.
-
What does this program print?
h = Hash.new(0) h["a"] += 1 h["a"] += 1 p hAnswer
{"a" => 2}
The default of zero turns the first increment into a real value, which is the counter pattern of the lesson.
-
What does this program print?
p format("%-12s %d", "/api/orders", 2)Answer
"/api/orders 2"
The name is padded to twelve columns, so two spaces sit between it and the count.
-
What does this program print?
p "x\n".match?(/x\z/) p "x\n".chomp.match?(/x\z/)Answer
false / true
The newline fails the string anchor, and chomp removes it before the second test.
-
What does this program print?
p "2026-01-01 00:00:00 GET /a 500 1".match(/\A(?<time>\S+ \S+)/)[:time]Answer
"2026-01-01 00:00:00"
The two non-space fields at the start are the date and the time, and the named group holds both.
-
Why is
ARGV.fetch(0)better thanARGV[0]?Answer
It makes a missing argument an error instead of reading standard input by accident
A pipeline stage should fail clearly when it is called without its input.
-
Why does the report sort with
[-count, path]rather than by the count alone?Answer
Two paths with the same count still come out in a stable order
Stable output is what makes a diff between two runs mean a real change.
-
Why does the script count the lines it skips?
Answer
A parser that hides bad input hides the bug
A line that does not parse is a number in the report, not a silent next.
-
Which line is the reason
line.chompis needed before a\zanchor?Answer
The newline at the end of the line otherwise fails the anchor
\z matches the end of the string, and the newline is still part of the line.
-
How does a pipeline version of the script fail a build when errors are present?
Answer
It exits with a non-zero status, as in
exit 1 unless errors.empty?The exit code is the second interface of a pipeline step, next to its output.
Article author: Arthur Isaev