📖 Scenario: You are managing a simple text file that stores a list of favorite fruits. You want to practice reading from, writing to, and appending to this file using Ruby's IO modes.
🎯 Goal: Build a Ruby program that reads the initial content of a file, overwrites it with new content, then appends additional content, demonstrating the use of r, w, and a modes.
📋 What You'll Learn
Create a text file named fruits.txt with initial fruit names.
Read and display the content of fruits.txt using r mode.
Overwrite fruits.txt with new fruit names using w mode.
Append more fruit names to fruits.txt using a mode.
Print the final content of fruits.txt after all operations.
💡 Why This Matters
🌍 Real World
Managing text files is common for saving logs, user data, or configuration settings in many applications.
💼 Career
Understanding file input/output modes is essential for backend developers, data engineers, and anyone working with file systems.
Progress0 / 4 steps
1
Create the initial file with fruit names
Create a file named fruits.txt and write these exact lines into it: Apple, Banana, and Cherry, each on its own line. Use Ruby's File.open with mode "w" and puts to write the lines.
Ruby
Hint
Use File.open with mode "w" to create and write to the file. Use puts to write each fruit on a new line.
2
Read and display the file content
Open fruits.txt in read mode "r" and read all lines into a variable called content. Then close the file.
Ruby
Hint
Use File.open with mode "r" to read the file. Use read to get all content as a string. Don't forget to close the file.
3
Overwrite the file with new fruits
Open fruits.txt in write mode "w" and overwrite it with these exact fruits: Orange and Grape, each on its own line. Close the file after writing.
Ruby
Hint
Use File.open with mode "w" again to overwrite the file. Write the new fruits with puts.
4
Append more fruits and print final content
Open fruits.txt in append mode "a" and add these fruits: Kiwi and Mango, each on its own line. Then open the file in read mode "r" and print the entire content stored in final_content.
Ruby
Hint
Use File.open with mode "a" to add lines without erasing. Then read the file again and print the content.
Practice
(1/5)
1. What does the 'r' mode do when opening a file in Ruby?
easy
A. Opens the file for reading only, without changing its content.
B. Opens the file for writing and erases existing content.
C. Opens the file for appending new content at the end.
D. Creates a new file if it doesn't exist and writes to it.
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 A
Quick Check:
'r' = read only [OK]
Hint: Remember 'r' stands for read only, no changes made [OK]
Common Mistakes:
Confusing 'r' with 'w' and thinking it erases content
Assuming 'r' can write to the file
Mixing 'r' with 'a' mode functionality
2. Which of the following is the correct syntax to open a file named data.txt for appending in Ruby?
easy
A. File.open('data.txt', 'x')
B. File.open('data.txt', 'w')
C. File.open('data.txt', 'r')
D. File.open('data.txt', 'a')
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 D
Quick Check:
Append mode = 'a' [OK]
Hint: Use 'a' to add content without erasing [OK]
Common Mistakes:
Using 'r' which is read-only mode
Using 'w' which erases file content
Using 'x' which creates a new file and errors if exists
3. What will be the output of this Ruby code if example.txt initially contains "Hello"?
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 C
Quick Check:
'w' erases old content, so output is new content with newline [OK]
Hint: Writing with 'w' erases old content, so only new text shows [OK]
Common Mistakes:
Thinking 'w' appends instead of overwriting
Expecting old content to remain
Confusing output with combined old and new text
4. The following Ruby code is intended to add the line "Goodbye" to the end of log.txt. What is the error?
File.open('log.txt', 'w') do |file|
file.puts 'Goodbye'
end
medium
A. The file name should be in double quotes, not single quotes.
B. Using 'w' mode erases the file instead of appending.
C. Missing a block parameter for the file object.
D. The method 'puts' cannot be used inside File.open.
Solution
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 B
Quick Check:
Append needs 'a', not 'w' [OK]
Hint: Use 'a' to add lines, 'w' erases file [OK]
Common Mistakes:
Confusing 'w' with append mode
Thinking 'w' adds content without erasing
Believing quotes style affects file opening
5. You want to create a Ruby script that reads a file 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?
hard
A. Open with 'r' to read, then 'a' to append, then 'r' to read again.
B. Open with 'a' to append, then 'r' to read the updated content.
C. Open with 'w' to write, then 'a' to append, then 'r' to read.
D. Open with 'r' to read, then 'w' to write, then 'r' to read again.
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 A
Quick Check:
Read, append, read = 'r', 'a', 'r' [OK]
Hint: Read first, append with 'a', then read again [OK]
Common Mistakes:
Using 'w' which erases content before appending
Trying to append before reading original content
Not reopening file to read updated content
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.