Text and files in both
The log file
The task is the smallest useful script: read an access log, count the lines that ended with a server error, write the count into a report and print it. The same five lines of log feed both versions.
access.log
127.0.0.1 - - [04/Nov/2026:10:00:00 +0000] "GET /index.html HTTP/1.1" 200 512
127.0.0.1 - - [04/Nov/2026:10:00:01 +0000] "GET /missing HTTP/1.1" 404 153
127.0.0.1 - - [04/Nov/2026:10:00:02 +0000] "GET /boom HTTP/1.1" 500 0
127.0.0.1 - - [04/Nov/2026:10:00:03 +0000] "POST /save HTTP/1.1" 500 0
127.0.0.1 - - [04/Nov/2026:10:00:04 +0000] "GET /index.html HTTP/1.1" 200 512
Ruby
lines = File.readlines("access.log") errors = lines.count { |line| line.include?(" 500 ") } File.write("report.txt", "errors: #{errors}\n") puts errors
ruby count.rb
2
The report it wrote
cat report.txt
errors: 2
Python
from pathlib import Path lines = Path("access.log").read_text().splitlines() errors = sum(1 for line in lines if " 500 " in line) Path("report.txt").write_text(f"errors: {errors}\n") print(errors)
python3 count.py
2
The report it wrote
cat report.txt
errors: 2
Both scripts print the same number and leave the same file behind. The next run of either one overwrites the report, which is the behaviour the task wants and a good reason to keep the output path in one place.
What differs
- The file API. Ruby has File.readlines and File.write on the class itself. Python usually goes through pathlib.Path, which ties the path and the read together: Path("access.log").read_text(). Both are in the standard library.
- Counting. Ruby's count takes a block and reads as a sentence. Python has no block syntax, so the same idea is sum(1 for line in lines if ...): a generator expression inside a call.
- Line endings. readlines keeps the \n at the end of each line; Python's splitlines() drops it. A == comparison on the whole line works in Python and fails in Ruby until the line is chomped, which is why the Ruby version tests include? here.
Neither API is faster in a way this task can see; both read the file in one go. For a file that does not fit in memory, both languages offer a line-by-line loop, and that is the version to write when the log is a gigabyte.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the difference between Ruby's
readlinesand Python'ssplitlines()?Answer
readlines keeps the newline and splitlines drops it
The Ruby line still ends with \n, which is why a whole-line comparison in Ruby needs chomp and the Python one does not.
-
What does this program print?
lines = ["a 200 b", "a 500 b", "c 500 d"] errors = lines.count { |line| line.include?(" 500 ") } puts errorsAnswer
2
Two lines have the status surrounded by spaces, which is the test the log script makes.
-
What does this program print?
line = "hello\n" p line == "hello" p line.chomp == "hello"Answer
false / true
The newline is part of the first string, and chomp removes it before the comparison.
-
What does this program print?
text = "a\nb\n" p text.lines.map(&:chomp)Answer
["a", "b"]
lines splits on the breaks and chomp removes each one, which is the Ruby spelling of splitlines.
-
What does this program print?
File.write("access.log", "x 500 y\nx 200 y\n") p File.readlines("access.log").sizeAnswer
2
The file has two lines, and the program writes it first so the count has an answer.
-
How does the Python version usually read a file?
Answer
Through pathlib.Path, which ties the path and the read together
Path("access.log").read_text() is the standard-library form, while Ruby calls File.readlines on the class.
-
What does this program print?
lines = [" 500 ", " 200 "] p lines.select { |l| l.include?(" 500 ") }.sizeAnswer
1
One line matches, and size turns the selection into the number the report needs.
-
What does this program print?
File.write("report.txt", "errors: 2\n") p File.read("report.txt").chompAnswer
"errors: 2"
The file ends with a newline and chomp removes it, and p shows the string with its quotes.
-
Python has no block syntax. How does the lesson count the matching lines?
Answer
With a generator expression inside sum, as in sum(1 for line in lines if ...)
The generator expression is the Python shape of a block that the language does not have.
-
What does this program print?
p "line 500 0\n".include?(" 500 ")Answer
true
The status in an access log sits between spaces, and the line has one after it.
Article author: Arthur Isaev