0
0
Rubyprogramming~10 mins

IO modes (r, w, a) in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aw
Br
Ca
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' will erase the file content.
Using 'a' opens the file for appending, not reading.
2fill in blank
medium

Complete 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'
Aa
Br
Cw
Dr+
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.
3fill in blank
hard

Fix 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'
Aw
Bw+
Cr
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' erases the file content.
Using 'r' opens for reading only, no writing allowed.
4fill in blank
hard

Fill 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'
Ar+
Bw
Cwritable?
Dreadable?
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' erases the file content.
Checking readable? does not confirm writing permission.
5fill in blank
hard

Fill 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'
Aa
Bwrite
Cclose
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' erases existing content.
Forgetting to close the file can cause data loss.