IO modes (r, w, a) in Ruby - Time & Space Complexity
When working with files in Ruby, we use different modes to read, write, or add data. Understanding how the time to complete these actions grows with file size helps us write better programs.
We want to know: how does the time to read, write, or append change as the file gets bigger?
Analyze the time complexity of the following code snippet.
# Open a file in read mode and read all lines
File.open('data.txt', 'r') do |file|
lines = file.readlines
end
# Open a file in write mode and write lines
File.open('data.txt', 'w') do |file|
file.puts("Hello World")
end
# Open a file in append mode and add a line
File.open('data.txt', 'a') do |file|
file.puts("New line")
end
This code reads all lines from a file, writes new content replacing old data, and appends a new line at the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading all lines from the file (in read mode) involves going through each line one by one.
- How many times: The reading loop runs once for each line in the file.
- Writing and appending write data once, no loops involved.
When reading, the time grows as the file gets bigger because each line is read one after another.
| Input Size (lines) | Approx. Operations |
|---|---|
| 10 | About 10 line reads |
| 100 | About 100 line reads |
| 1000 | About 1000 line reads |
Pattern observation: Reading time grows directly with the number of lines. Writing or appending a single line stays about the same time regardless of file size.
Time Complexity: O(n)
This means reading takes longer as the file grows, because it reads each line once. Writing or appending a small amount is constant time.
[X] Wrong: "Writing to a file always takes longer as the file gets bigger."
[OK] Correct: Writing with mode 'w' replaces the file, so time depends on what you write, not the old file size. Appending adds data at the end, usually quickly.
Knowing how file operations scale helps you handle data efficiently in real projects. It shows you understand how programs interact with files and manage time well.
"What if we read the file line by line instead of all at once? How would the time complexity change?"
Practice
'r' mode do when opening a file in Ruby?Solution
Step 1: Understand the purpose of 'r' mode
The 'r' mode is used to open a file only for reading, so the file content is not changed.Step 2: Compare with other modes
'w' mode writes and erases content, 'a' appends, but 'r' only reads.Final Answer:
Opens the file for reading only, without changing its content. -> Option AQuick Check:
'r' = read only [OK]
- Confusing 'r' with 'w' and thinking it erases content
- Assuming 'r' can write to the file
- Mixing 'r' with 'a' mode functionality
data.txt for appending in Ruby?Solution
Step 1: Identify the mode for appending
The mode 'a' is used to open a file for appending new content at the end.Step 2: Check the syntax
The correct syntax is File.open('filename', 'a') to append.Final Answer:
File.open('data.txt', 'a') -> Option DQuick Check:
Append mode = 'a' [OK]
- Using 'r' which is read-only mode
- Using 'w' which erases file content
- Using 'x' which creates a new file and errors if exists
example.txt initially contains "Hello"?File.open('example.txt', 'w') { |f| f.puts 'World' }
content = File.read('example.txt')
puts contentSolution
Step 1: Understand 'w' mode effect
Opening with 'w' erases existing content and writes new content.Step 2: Analyze the code output
The file is overwritten with "World" and then read back, so output is "World" with a newline.Final Answer:
World -> Option CQuick Check:
'w' erases old content, so output is new content with newline [OK]
- Thinking 'w' appends instead of overwriting
- Expecting old content to remain
- Confusing output with combined old and new text
log.txt. What is the error?File.open('log.txt', 'w') do |file|
file.puts 'Goodbye'
endSolution
Step 1: Identify the mode used
The code uses 'w' mode which overwrites the file content.Step 2: Understand the intended action
To add content without erasing, 'a' mode should be used instead of 'w'.Final Answer:
Using 'w' mode erases the file instead of appending. -> Option BQuick Check:
Append needs 'a', not 'w' [OK]
- Confusing 'w' with append mode
- Thinking 'w' adds content without erasing
- Believing quotes style affects file opening
notes.txt, appends the line "New note added", and then reads the updated content to print it. Which sequence of file modes should you use?Solution
Step 1: Read the original file
Use 'r' mode to read the existing content without changing it.Step 2: Append the new line
Use 'a' mode to add "New note added" at the end without erasing.Step 3: Read the updated content
Open again with 'r' mode to read and print the updated file content.Final Answer:
Open with 'r' to read, then 'a' to append, then 'r' to read again. -> Option AQuick Check:
Read, append, read = 'r', 'a', 'r' [OK]
- Using 'w' which erases content before appending
- Trying to append before reading original content
- Not reopening file to read updated content
Ready to prove it?
Pass the test and this pattern moves unknown
