Recall & Review
beginner
What does
File.write do in Ruby?File.write writes the given content to a file. If the file doesn't exist, it creates it. If it exists, it overwrites the content.
Click to reveal answer
beginner
How do you write the string "Hello" to a file named "greeting.txt" using
File.write?File.write("greeting.txt", "Hello")Click to reveal answer
beginner
What happens if you use
File.write on an existing file?The existing file's content is replaced with the new content you provide.
Click to reveal answer
intermediate
Can
File.write append content to a file?No, File.write overwrites the file. To append, you need to use other methods like File.open with append mode.
Click to reveal answer
intermediate
What value does
File.write return after writing to a file?It returns the number of bytes written to the file.
Click to reveal answer
What does
File.write('example.txt', 'Hi') do if 'example.txt' does not exist?✗ Incorrect
File.write creates the file if it doesn't exist and writes the content.
If 'data.txt' contains 'Old', what will
File.write('data.txt', 'New') do?✗ Incorrect
File.write overwrites existing content.
What does
File.write return after writing to a file?✗ Incorrect
It returns the number of bytes written to the file.
Which method should you use to add content to the end of a file instead of overwriting?
✗ Incorrect
Use File.open with append mode ('a') to add content.
What happens if you pass a non-string object to
File.write as content?✗ Incorrect
File.write converts the content to string before writing.
Explain how
File.write works for writing content to files in Ruby.Think about what happens if the file exists or not.
You got /3 concepts.
Describe the difference between
File.write and appending content to a file.Consider how to keep old content and add new content.
You got /3 concepts.