Bird
Raised Fist0
Rubyprogramming~20 mins

Why file handling matters in Ruby - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File Handling Mastery
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 that writes and reads a file?

Consider this Ruby code that writes a string to a file and then reads it back. What will it print?

Ruby
File.open('test.txt', 'w') do |file|
  file.puts 'Hello, file handling!'
end

content = File.read('test.txt')
puts content.strip
AError: No such file or directory
BHello, file handling!
Ctest.txt
Dnil
Attempts:
2 left
💡 Hint

Think about what file.puts does and how File.read works.

🧠 Conceptual
intermediate
1:30remaining
Why is it important to close files after opening them in Ruby?

Why should you always close a file after opening it in Ruby?

ATo speed up the computer's processor
BTo make the file invisible on the disk
CTo delete the file automatically
DTo free system resources and avoid file corruption
Attempts:
2 left
💡 Hint

Think about what happens if too many files stay open.

🔧 Debug
advanced
2:00remaining
What error does this Ruby code raise when trying to read a non-existent file?

Look at this Ruby code trying to read a file that does not exist. What error will it raise?

Ruby
content = File.read('missing_file.txt')
puts content
AErrno::ENOENT (No such file or directory @ rb_sysopen - missing_file.txt)
BNoMethodError: undefined method 'read' for File
CSyntaxError: unexpected end-of-input
DTypeError: no implicit conversion of nil into String
Attempts:
2 left
💡 Hint

What happens when Ruby tries to open a file that isn't there?

📝 Syntax
advanced
2:00remaining
Which Ruby code snippet correctly appends text to an existing file?

Choose the Ruby code that correctly adds the line 'Appended line' to the end of 'log.txt'.

AFile.open('log.txt', 'w') { |file| file.puts 'Appended line' }
BFile.read('log.txt') << 'Appended line'
CFile.open('log.txt', 'a') { |file| file.puts 'Appended line' }
DFile.write('log.txt', 'Appended line')
Attempts:
2 left
💡 Hint

Remember, 'a' mode means append, 'w' means overwrite.

🚀 Application
expert
3:00remaining
How many lines will the file contain after running this Ruby code?

This Ruby code writes three lines to a file, then reads and appends one more line. How many lines will the file have at the end?

Ruby
File.open('data.txt', 'w') do |file|
  file.puts 'Line 1'
  file.puts 'Line 2'
  file.puts 'Line 3'
end

lines = File.readlines('data.txt')
File.open('data.txt', 'a') { |file| file.puts "Line #{lines.size + 1}" }
A4
B3
C1
D5
Attempts:
2 left
💡 Hint

Count the lines written first, then the appended line.

Practice

(1/5)
1. Why is file handling important in Ruby programming?
easy
A. It allows saving and loading data outside the program
B. It speeds up the program execution automatically
C. It makes the program use less memory
D. It changes the program's user interface

Solution

  1. Step 1: Understand file handling purpose

    File handling lets programs save data to files and read data back later.
  2. Step 2: Identify correct purpose from options

    Only It allows saving and loading data outside the program describes saving and loading data outside the program.
  3. Final Answer:

    It allows saving and loading data outside the program -> Option A
  4. Quick Check:

    File handling = save/load data [OK]
Hint: File handling means saving or reading data outside program [OK]
Common Mistakes:
  • Thinking file handling speeds up code
  • Confusing file handling with memory management
  • Believing file handling changes UI
2. Which Ruby code correctly opens a file for writing using a block?
easy
A. File.read('data.txt', 'w') do |file| file.puts 'Hello' end
B. File.open('data.txt', 'r') do |file| file.puts 'Hello' end
C. File.open('data.txt', 'w') do |file| file.puts 'Hello' end
D. File.write('data.txt', 'w') do |file| file.puts 'Hello' end

Solution

  1. Step 1: Identify correct method and mode for writing

    File.open with mode 'w' opens file for writing; block syntax ensures file closes.
  2. Step 2: Check each option's correctness

    File.open('data.txt', 'w') do |file| file.puts 'Hello' end uses File.open with 'w' and block correctly; others misuse methods or modes.
  3. Final Answer:

    File.open('data.txt', 'w') do |file|\n file.puts 'Hello'\nend -> Option C
  4. Quick Check:

    File.open + 'w' + block = correct syntax [OK]
Hint: Use File.open with 'w' mode and a block to write safely [OK]
Common Mistakes:
  • Using File.read instead of File.open for writing
  • Using wrong mode like 'r' for writing
  • Using File.write with a block (not supported)
3. What will be the output of this Ruby code?
File.open('test.txt', 'w') { |f| f.puts 'Line 1' }
content = File.read('test.txt')
puts content
medium
A. Error: file not found
B. test.txt
C. nil
D. Line 1

Solution

  1. Step 1: Write 'Line 1' to file using block

    The block writes 'Line 1' plus newline to 'test.txt' and closes file.
  2. Step 2: Read file content and print

    File.read reads the file content 'Line 1\n', puts prints it as 'Line 1'.
  3. Final Answer:

    Line 1 -> Option D
  4. Quick Check:

    Write then read file = output written text [OK]
Hint: Write then read file prints what was saved [OK]
Common Mistakes:
  • Expecting filename instead of content
  • Thinking output is nil
  • Assuming file not found error
4. What is wrong with this Ruby code snippet?
file = File.open('data.txt', 'r')
file.puts 'Hello'
file.close
medium
A. File opened in read mode but trying to write
B. File not closed properly
C. Missing block for File.open
D. File name is invalid

Solution

  1. Step 1: Check file mode and operation

    File opened with 'r' (read) mode but code tries to write with puts, which is not allowed.
  2. Step 2: Confirm other options

    File is closed properly; block is optional; filename looks valid.
  3. Final Answer:

    File opened in read mode but trying to write -> Option A
  4. Quick Check:

    Read mode + write operation = error [OK]
Hint: Don't write to file opened in read mode [OK]
Common Mistakes:
  • Ignoring file mode mismatch
  • Thinking block is always required
  • Assuming file not closed
5. You want to read a file and count how many lines contain the word 'error'. Which Ruby code correctly does this using file handling?
hard
A. count = 0 file = File.open('log.txt', 'w') file.each_line do |line| count += 1 if line.include?('error') end file.close puts count
B. count = 0 File.open('log.txt', 'r') do |file| file.each_line do |line| count += 1 if line.include?('error') end end puts count
C. count = 0 File.read('log.txt').each do |line| count += 1 if line.include?('error') end puts count
D. count = 0 File.open('log.txt') do |file| file.read.each do |line| count += 1 if line.include?('error') end end puts count

Solution

  1. Step 1: Identify correct file mode and iteration

    count = 0 File.open('log.txt', 'r') do |file| file.each_line do |line| count += 1 if line.include?('error') end end puts count opens file in 'r' mode and uses each_line with block, counting lines with 'error'.
  2. Step 2: Check other options for errors

    count = 0 file = File.open('log.txt', 'w') file.each_line do |line| count += 1 if line.include?('error') end file.close puts count opens file in 'w' (write) mode, wrong for reading; count = 0 File.read('log.txt').each do |line| count += 1 if line.include?('error') end puts count uses File.read but does not handle string properly; count = 0 File.open('log.txt') do |file| file.read.each do |line| count += 1 if line.include?('error') end end puts count misses mode argument in File.open.
  3. Final Answer:

    count = 0 File.open('log.txt', 'r') do |file| file.each_line do |line| count += 1 if line.include?('error') end end puts count -> Option B
  4. Quick Check:

    Open file read + each_line + count lines with 'error' [OK]
Hint: Open file in 'r' mode and use each_line to count matches [OK]
Common Mistakes:
  • Opening file in write mode when reading
  • Not specifying mode in File.open
  • Using File.read without splitting lines
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