0
0
Rubyprogramming~10 mins

File.readlines for line-by-line in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - File.readlines for line-by-line
Open file
Read all lines into array
Iterate over array
Process each line
Close file
The program opens a file, reads all lines into an array, then processes each line one by one.
Execution Sample
Ruby
lines = File.readlines('example.txt')
lines.each do |line|
  puts line.chomp
end
Reads all lines from 'example.txt' into an array and prints each line without extra newline.
Execution Table
StepActionEvaluationResult
1Call File.readlines('example.txt')Reads entire fileArray of lines: ["Hello\n", "World\n", "Ruby\n"]
2Start iteration over arrayFirst element: "Hello\n"line = "Hello\n"
3Print line.chompRemove trailing newlineOutput: Hello
4Next iterationSecond element: "World\n"line = "World\n"
5Print line.chompRemove trailing newlineOutput: World
6Next iterationThird element: "Ruby\n"line = "Ruby\n"
7Print line.chompRemove trailing newlineOutput: Ruby
8End iterationNo more elementsIteration stops
💡 All lines processed, iteration ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
linesnil["Hello\n", "World\n", "Ruby\n"]["Hello\n", "World\n", "Ruby\n"]["Hello\n", "World\n", "Ruby\n"]["Hello\n", "World\n", "Ruby\n"]
linenil"Hello\n""World\n""Ruby\n"nil
Key Moments - 3 Insights
Why does each line include a newline character at first?
File.readlines reads each line including the newline character '\n', as shown in execution_table step 1 where lines contain '\n'.
Why do we use line.chomp when printing?
line.chomp removes the trailing newline '\n' so the output lines don't have extra blank lines, as seen in steps 3, 5, and 7.
Does File.readlines keep the file open during iteration?
No, File.readlines reads all lines at once and closes the file immediately, so iteration works on the array in memory (step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'line' at step 4?
A"Hello\n"
B"World\n"
C"Ruby\n"
Dnil
💡 Hint
Check the 'Action' and 'Evaluation' columns at step 4 in execution_table.
At which step does the iteration stop because there are no more lines?
AStep 6
BStep 7
CStep 8
DStep 3
💡 Hint
Look for the step with 'End iteration' and 'No more elements' in execution_table.
If the file had 5 lines instead of 3, how would the variable 'lines' change in variable_tracker?
AIt would have 5 elements in the array instead of 3
BIt would still have 3 elements
CIt would be nil
DIt would be a string, not an array
💡 Hint
Variable 'lines' holds all lines read; more lines means a longer array (see variable_tracker).
Concept Snapshot
File.readlines('filename') reads all lines into an array.
Each element includes the newline character '\n'.
Use .each to loop line-by-line.
Use line.chomp to remove trailing newline when printing.
File is closed after reading all lines.
Full Transcript
This example shows how Ruby's File.readlines method reads all lines from a file into an array. Each line includes a newline character at the end. We then loop over this array using each, processing one line at a time. Using line.chomp removes the newline for clean output. The file is opened and closed automatically during the read. The variable 'lines' holds the array of all lines, and 'line' holds the current line during iteration. The iteration stops when all lines are processed.