Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open a file for reading.
Ruby
file = File.open('example.txt', '[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' will erase the file content.
Using 'a' opens the file for appending, not reading.
✗ Incorrect
Use mode 'r' to open a file for reading only.
2fill in blank
mediumComplete the code to open a file for writing (overwriting existing content).
Ruby
file = File.open('output.txt', '[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' will cause an error if the file does not exist.
Using 'a' will append instead of overwrite.
✗ Incorrect
Use mode 'w' to open a file for writing and overwrite existing content.
3fill in blank
hardFix the error in the code to open a file for appending text.
Ruby
file = File.open('log.txt', '[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' erases the file content.
Using 'r' opens for reading only, no writing allowed.
✗ Incorrect
Use mode 'a' to open a file for appending text at the end.
4fill in blank
hardFill both blanks to open a file for reading and writing without erasing content.
Ruby
file = File.open('data.txt', '[1]') file.write('Hello') if file.[2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' erases the file content.
Checking
readable? does not confirm writing permission.✗ Incorrect
Mode 'r+' opens for reading and writing without erasing. Use writable? to check if writing is allowed.
5fill in blank
hardFill all three blanks to open a file for appending, write a line, and close the file.
Ruby
file = File.open('notes.txt', '[1]') file.[2]('New note\n') file.[3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' erases existing content.
Forgetting to close the file can cause data loss.
✗ Incorrect
Use mode 'a' to append, write to add text, and close to save and close the file.