Bird
Raised Fist0
Rubyprogramming~20 mins

File.write for writing in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File Write Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using File.write?

Consider this Ruby code that writes to a file and then reads it back:

File.write('test.txt', 'Hello')
content = File.read('test.txt')
puts content

What will be printed?

Ruby
File.write('test.txt', 'Hello')
content = File.read('test.txt')
puts content
Atest.txt
Bnil
CHello
DError: file not found
Attempts:
2 left
💡 Hint

File.write writes the string to the file, and File.read reads it back.

❓ Predict Output
intermediate
2:00remaining
What does File.write return in Ruby?

Look at this Ruby code:

result = File.write('output.txt', 'Data')

What is the value of result after this runs?

Ruby
result = File.write('output.txt', 'Data')
AThe number of bytes written (4)
BThe string 'Data'
Ctrue
Dnil
Attempts:
2 left
💡 Hint

File.write returns how many bytes it wrote to the file.

🔧 Debug
advanced
2:00remaining
Why does this File.write code raise an error?

What error does this Ruby code raise?

File.write(123, 'Hello')
Ruby
File.write(123, 'Hello')
AArgumentError: wrong number of arguments
BTypeError: no implicit conversion of Integer into String
CErrno::ENOENT: No such file or directory
DNo error, writes file named '123'
Attempts:
2 left
💡 Hint

File.write expects a string filename, not a number.

📝 Syntax
advanced
2:00remaining
Which option correctly appends text to a file using File.write?

You want to add text to the end of an existing file. Which Ruby code does this correctly?

AFile.write('log.txt', 'New entry', append: true)
BFile.write('log.txt', 'New entry', 'a')
CFile.write('log.txt', 'New entry', flags: 'w')
DFile.write('log.txt', 'New entry', mode: 'a')
Attempts:
2 left
💡 Hint

File.write accepts options as a hash, and mode: 'a' means append.

🚀 Application
expert
3:00remaining
How many bytes are written and what is the file content after this code?

Consider this Ruby code:

bytes = File.write('data.txt', "Line1\nLine2\n", mode: 'w')
content = File.read('data.txt')

What are the values of bytes and content?

Ruby
bytes = File.write('data.txt', "Line1\nLine2\n", mode: 'w')
content = File.read('data.txt')
Abytes = 12, content = "Line1\nLine2\n"
Bbytes = 11, content = "Line1\nLine2"
Cbytes = 12, content = "Line1 Line2"
Dbytes = 10, content = "Line1\nLine2\n"
Attempts:
2 left
💡 Hint

Count characters including newline characters. File.write returns bytes written exactly.

Practice

(1/5)
1. What does the Ruby method File.write do by default when writing to a file?
easy
A. It creates the file if it doesn't exist and overwrites existing content.
B. It appends new content to the end of the file without changing existing content.
C. It only reads the file content without modifying it.
D. It deletes the file before writing new content.

Solution

  1. Step 1: Understand File.write default behavior

    By default, File.write creates the file if it does not exist.
  2. Step 2: Check how it handles existing files

    If the file exists, it overwrites the entire content with the new data.
  3. Final Answer:

    It creates the file if it doesn't exist and overwrites existing content. -> Option A
  4. Quick Check:

    File.write overwrites by default = A [OK]
Hint: Remember: File.write overwrites unless mode is changed [OK]
Common Mistakes:
  • Thinking File.write appends by default
  • Assuming File.write only reads files
  • Believing File.write deletes files before writing
2. Which of the following is the correct syntax to write the string "Hello" to a file named "greet.txt" using File.write in Ruby?
easy
A. File.write("greet.txt") { "Hello" }
B. File.write("Hello", "greet.txt")
C. File.write("greet.txt", "Hello")
D. File.write("greet.txt" => "Hello")

Solution

  1. Step 1: Identify File.write parameters

    The first argument is the filename, the second is the content string.
  2. Step 2: Match correct syntax

    File.write("greet.txt", "Hello") correctly uses File.write("greet.txt", "Hello").
  3. Final Answer:

    File.write("greet.txt", "Hello") -> Option C
  4. Quick Check:

    File.write(filename, content) = C [OK]
Hint: Filename first, content second in File.write [OK]
Common Mistakes:
  • Swapping filename and content arguments
  • Using block syntax incorrectly
  • Passing a hash instead of arguments
3. What will be the content of the file "data.txt" after running this Ruby code?
File.write("data.txt", "Line 1\n")
File.write("data.txt", "Line 2\n")
medium
A. Line 2\n
B. Line 1\nLine 2\n
C. Line 1\n
D. Line 1\n\nLine 2\n

Solution

  1. Step 1: Analyze first File.write call

    It writes "Line 1\n" to "data.txt", creating or overwriting the file.
  2. Step 2: Analyze second File.write call

    It overwrites the entire file content with "Line 2\n" because File.write overwrites by default.
  3. Final Answer:

    Line 2\n -> Option A
  4. Quick Check:

    File.write overwrites, so last write stays = B [OK]
Hint: Each File.write overwrites unless mode 'a' is set [OK]
Common Mistakes:
  • Assuming File.write appends by default
  • Expecting both lines to be in the file
  • Confusing newline characters with extra lines
4. The following Ruby code is intended to append "More text" to the file "notes.txt". What is the error?
File.write("notes.txt", "More text")
medium
A. The filename should be a symbol, not a string.
B. File.write overwrites instead of appending; mode 'a' is missing.
C. The string "More text" must be inside single quotes.
D. File.write cannot write strings, only bytes.

Solution

  1. Step 1: Understand File.write default mode

    By default, File.write overwrites the file content.
  2. Step 2: Identify how to append

    To append, you must specify mode: File.write("notes.txt", "More text", mode: "a").
  3. Final Answer:

    File.write overwrites instead of appending; mode 'a' is missing. -> Option B
  4. Quick Check:

    Append needs mode 'a' = A [OK]
Hint: Use mode: 'a' to append with File.write [OK]
Common Mistakes:
  • Not specifying mode to append
  • Using symbols instead of strings for filename
  • Thinking quotes style affects writing
  • Believing File.write can't write strings
5. You want to write multiple lines to a file "log.txt" without overwriting existing content. Which Ruby code correctly appends these lines using File.write?
hard
A. File.write("log.txt", "First line\nSecond line\n", append: true)
B. File.write("log.txt", "First line\nSecond line\n")
C. File.write("log.txt", "First line\nSecond line\n", mode: "w")
D. File.write("log.txt", "First line\nSecond line\n", mode: "a")

Solution

  1. Step 1: Identify how to append with File.write

    Appending requires mode set to "a" (append mode).
  2. Step 2: Check options for correct syntax

    File.write("log.txt", "First line\nSecond line\n", mode: "a") uses mode: "a" correctly to append multiple lines.
  3. Final Answer:

    File.write("log.txt", "First line\nSecond line\n", mode: "a") -> Option D
  4. Quick Check:

    Append mode is 'a' in File.write = D [OK]
Hint: Use mode: 'a' to append multiple lines safely [OK]
Common Mistakes:
  • Omitting mode for append
  • Using mode 'w' which overwrites
  • Using invalid option 'append: true'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard