0
0
Rubyprogramming~5 mins

File.write for writing in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ACreates 'example.txt' and writes 'Hi' inside
BThrows an error because the file doesn't exist
CAppends 'Hi' to the file
DDeletes the file
If 'data.txt' contains 'Old', what will File.write('data.txt', 'New') do?
AOverwrite 'Old' with 'New'
BAppend 'New' after 'Old'
CThrow an error
DCreate a new file named 'data.txt1'
What does File.write return after writing to a file?
AThe file name
BThe content written
CNothing (nil)
DThe number of bytes written
Which method should you use to add content to the end of a file instead of overwriting?
AFile.write
BFile.read
CFile.open with append mode
DFile.delete
What happens if you pass a non-string object to File.write as content?
AIt writes the object as is
BIt converts the object to string and writes it
CIt throws an error
DIt ignores the content
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.