0
0
Rubyprogramming~5 mins

IO modes (r, w, a) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A'r' (read)
B'a' (append)
C'w' (write)
D'x' (exclusive creation)
What happens if you open a file with 'w' mode and the file already exists?
AAn error is raised.
BThe file is opened for reading only.
CThe file is opened without changing content.
DThe file is cleared (emptied) before writing.
If you want to read a file, which mode should you use?
A'w'
B'a'
C'r'
D'x'
What will happen if you try to open a file that does not exist with 'r' mode?
AAn error is raised.
BThe file is appended.
CThe file is opened for writing.
DThe file is created.
Which mode creates a new file if it does not exist and opens it for writing?
A'w'
B'r'
C'a'
D'r+'
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.