Why file handling matters in Ruby - Performance Analysis
When working with files, it is important to know how the time to read or write grows as the file size changes.
We want to understand how the program's speed changes when handling bigger or smaller files.
Analyze the time complexity of the following code snippet.
File.open('data.txt', 'r') do |file|
file.each_line do |line|
puts line.chomp
end
end
This code reads a file line by line and prints each line without extra spaces.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line from the file one by one.
- How many times: Once for every line in the file.
As the file gets bigger, the program reads more lines, so it takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 lines | 10 reads and prints |
| 100 lines | 100 reads and prints |
| 1000 lines | 1000 reads and prints |
Pattern observation: The time grows directly with the number of lines; double the lines, double the work.
Time Complexity: O(n)
This means the time to read and print grows in a straight line with the number of lines in the file.
[X] Wrong: "Reading a file always takes the same time no matter how big it is."
[OK] Correct: The program reads each line one by one, so bigger files take more time to process.
Understanding how file size affects reading time helps you write programs that handle data efficiently and predict performance.
"What if we read the whole file at once instead of line by line? How would the time complexity change?"
Practice
Solution
Step 1: Understand file handling purpose
File handling lets programs save data to files and read data back later.Step 2: Identify correct purpose from options
Only It allows saving and loading data outside the program describes saving and loading data outside the program.Final Answer:
It allows saving and loading data outside the program -> Option AQuick Check:
File handling = save/load data [OK]
- Thinking file handling speeds up code
- Confusing file handling with memory management
- Believing file handling changes UI
Solution
Step 1: Identify correct method and mode for writing
File.open with mode 'w' opens file for writing; block syntax ensures file closes.Step 2: Check each option's correctness
File.open('data.txt', 'w') do |file| file.puts 'Hello' end uses File.open with 'w' and block correctly; others misuse methods or modes.Final Answer:
File.open('data.txt', 'w') do |file|\n file.puts 'Hello'\nend -> Option CQuick Check:
File.open + 'w' + block = correct syntax [OK]
- Using File.read instead of File.open for writing
- Using wrong mode like 'r' for writing
- Using File.write with a block (not supported)
File.open('test.txt', 'w') { |f| f.puts 'Line 1' }
content = File.read('test.txt')
puts contentSolution
Step 1: Write 'Line 1' to file using block
The block writes 'Line 1' plus newline to 'test.txt' and closes file.Step 2: Read file content and print
File.read reads the file content 'Line 1\n', puts prints it as 'Line 1'.Final Answer:
Line 1 -> Option DQuick Check:
Write then read file = output written text [OK]
- Expecting filename instead of content
- Thinking output is nil
- Assuming file not found error
file = File.open('data.txt', 'r')
file.puts 'Hello'
file.closeSolution
Step 1: Check file mode and operation
File opened with 'r' (read) mode but code tries to write with puts, which is not allowed.Step 2: Confirm other options
File is closed properly; block is optional; filename looks valid.Final Answer:
File opened in read mode but trying to write -> Option AQuick Check:
Read mode + write operation = error [OK]
- Ignoring file mode mismatch
- Thinking block is always required
- Assuming file not closed
Solution
Step 1: Identify correct file mode and iteration
count = 0 File.open('log.txt', 'r') do |file| file.each_line do |line| count += 1 if line.include?('error') end end puts count opens file in 'r' mode and uses each_line with block, counting lines with 'error'.Step 2: Check other options for errors
count = 0 file = File.open('log.txt', 'w') file.each_line do |line| count += 1 if line.include?('error') end file.close puts count opens file in 'w' (write) mode, wrong for reading; count = 0 File.read('log.txt').each do |line| count += 1 if line.include?('error') end puts count uses File.read but does not handle string properly; count = 0 File.open('log.txt') do |file| file.read.each do |line| count += 1 if line.include?('error') end end puts count misses mode argument in File.open.Final Answer:
count = 0 File.open('log.txt', 'r') do |file| file.each_line do |line| count += 1 if line.include?('error') end end puts count -> Option BQuick Check:
Open file read + each_line + count lines with 'error' [OK]
- Opening file in write mode when reading
- Not specifying mode in File.open
- Using File.read without splitting lines
Ready to prove it?
Pass the test and this pattern moves unknown
