File.write for writing in Ruby - Time & Space Complexity
When we use File.write in Ruby, we want to know how the time it takes changes as the data size grows.
We ask: How does writing more data affect the time needed?
Analyze the time complexity of the following code snippet.
text = "Hello, world!" * n
File.write("output.txt", text)
This code creates a string repeated n times and writes it to a file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing each character of the string to the file.
- How many times: Once for each character in the string, which grows with
n.
As the string gets longer, the time to write grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Writing about 130 characters |
| 100 | Writing about 1,300 characters |
| 1000 | Writing about 13,000 characters |
Pattern observation: Doubling n roughly doubles the work needed to write the file.
Time Complexity: O(n)
This means the time to write grows directly with the size of the data.
[X] Wrong: "Writing a file always takes the same time no matter how big the data is."
[OK] Correct: Writing more data means more characters to save, so it takes longer as the data grows.
Understanding how file writing time grows helps you explain performance in real programs that save data.
"What if we wrote the file in small chunks inside a loop instead of all at once? How would the time complexity change?"
Practice
File.write do by default when writing to a file?Solution
Step 1: Understand File.write default behavior
By default,File.writecreates the file if it does not exist.Step 2: Check how it handles existing files
If the file exists, it overwrites the entire content with the new data.Final Answer:
It creates the file if it doesn't exist and overwrites existing content. -> Option AQuick Check:
File.write overwrites by default = A [OK]
- Thinking File.write appends by default
- Assuming File.write only reads files
- Believing File.write deletes files before writing
File.write in Ruby?Solution
Step 1: Identify File.write parameters
The first argument is the filename, the second is the content string.Step 2: Match correct syntax
File.write("greet.txt", "Hello") correctly usesFile.write("greet.txt", "Hello").Final Answer:
File.write("greet.txt", "Hello") -> Option CQuick Check:
File.write(filename, content) = C [OK]
- Swapping filename and content arguments
- Using block syntax incorrectly
- Passing a hash instead of arguments
File.write("data.txt", "Line 1\n")
File.write("data.txt", "Line 2\n")Solution
Step 1: Analyze first File.write call
It writes "Line 1\n" to "data.txt", creating or overwriting the file.Step 2: Analyze second File.write call
It overwrites the entire file content with "Line 2\n" because File.write overwrites by default.Final Answer:
Line 2\n -> Option AQuick Check:
File.write overwrites, so last write stays = B [OK]
- Assuming File.write appends by default
- Expecting both lines to be in the file
- Confusing newline characters with extra lines
File.write("notes.txt", "More text")Solution
Step 1: Understand File.write default mode
By default, File.write overwrites the file content.Step 2: Identify how to append
To append, you must specify mode: File.write("notes.txt", "More text", mode: "a").Final Answer:
File.write overwrites instead of appending; mode 'a' is missing. -> Option BQuick Check:
Append needs mode 'a' = A [OK]
- Not specifying mode to append
- Using symbols instead of strings for filename
- Thinking quotes style affects writing
- Believing File.write can't write strings
File.write?Solution
Step 1: Identify how to append with File.write
Appending requires mode set to "a" (append mode).Step 2: Check options for correct syntax
File.write("log.txt", "First line\nSecond line\n", mode: "a") uses mode: "a" correctly to append multiple lines.Final Answer:
File.write("log.txt", "First line\nSecond line\n", mode: "a") -> Option DQuick Check:
Append mode is 'a' in File.write = D [OK]
- Omitting mode for append
- Using mode 'w' which overwrites
- Using invalid option 'append: true'
Ready to prove it?
Pass the test and this pattern moves unknown
