Recall & Review
beginner
What does the 'r' mode do when opening a file in Ruby?
The 'r' mode opens a file for reading only. The file must exist, or Ruby will raise an error.
Click to reveal answer
beginner
What happens when you open a file with 'w' mode in Ruby?
The 'w' mode opens a file for writing. If the file exists, it is cleared (emptied). If it doesn't exist, a new file is created.
Click to reveal answer
beginner
Explain the 'a' mode in Ruby file handling.
The 'a' mode opens a file for appending. Data written is added to the end of the file. If the file doesn't exist, it is created.
Click to reveal answer
beginner
What will happen if you try to open a non-existent file with 'r' mode?
Ruby will raise an error because 'r' mode requires the file to exist for reading.
Click to reveal answer
beginner
How does 'w' mode affect existing file content?
Opening a file with 'w' mode deletes all existing content, making the file empty before writing new data.
Click to reveal answer
Which mode should you use to add text to the end of an existing file without deleting its content?
✗ Incorrect
The 'a' mode appends data to the end of the file without deleting existing content.
What happens if you open a file with 'w' mode and the file already exists?
✗ Incorrect
'w' mode clears the file content before writing new data.
If you want to read a file, which mode should you use?
✗ Incorrect
'r' mode opens the file for reading only.
What will happen if you try to open a file that does not exist with 'r' mode?
✗ Incorrect
'r' mode requires the file to exist; otherwise, Ruby raises an error.
Which mode creates a new file if it does not exist and opens it for writing?
✗ Incorrect
'w' mode creates a new file if it does not exist and opens it for writing.
Describe the differences between the 'r', 'w', and 'a' modes when opening files in Ruby.
Think about what happens to the file content and whether the file must exist.
You got /3 concepts.
What errors or behaviors should you expect when opening files with these modes if the file does not exist?
Consider which modes require the file beforehand and which create it.
You got /3 concepts.