📖 Scenario: Imagine you are creating a simple program to keep track of your daily to-do list. You want to save your tasks to a file so you can open the program later and see what you need to do. This is where file handling becomes important.
🎯 Goal: You will build a small Ruby program that writes a list of tasks to a file, reads the tasks back from the file, and shows them on the screen. This will help you understand why file handling matters in programming.
📋 What You'll Learn
Create a list of tasks as an array
Create a filename variable to store the file name
Write the tasks to the file using file handling
Read the tasks from the file and print them
💡 Why This Matters
🌍 Real World
Saving and loading data like to-do lists, notes, or settings is common in many programs. File handling lets programs remember information even after they close.
💼 Career
Understanding file handling is important for software developers, data analysts, and anyone who builds applications that store data persistently.
Progress0 / 4 steps
1
Create the list of tasks
Create an array called tasks with these exact strings: "Buy groceries", "Clean the house", "Pay bills"
Ruby
Hint
Use square brackets [] to create an array and separate items with commas.
2
Set the filename
Create a variable called filename and set it to the string "tasks.txt"
Ruby
Hint
Use a simple string assignment for the filename.
3
Write tasks to the file
Use File.open with filename and mode "w" to open the file for writing. Inside the block, write each task from tasks to the file on its own line using file.puts(task) in a for loop with variables task.
Ruby
Hint
Use a block with File.open and a for loop to write each task.
4
Read and print tasks from the file
Use File.open with filename and mode "r" to open the file for reading. Inside the block, use a for loop with variable line to read each line from the file and print it using print line.
Ruby
Hint
Use print line inside the loop to show each task exactly as saved.
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
Step 1: Understand file handling purpose
File handling lets programs save data to files and read data back later.
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.
Final Answer:
It allows saving and loading data outside the program -> Option A
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
Step 1: Identify correct method and mode for writing
File.open with mode 'w' opens file for writing; block syntax ensures file closes.
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.
Final Answer:
File.open('data.txt', 'w') do |file|\n file.puts 'Hello'\nend -> Option C
Quick Check:
File.open + 'w' + block = correct syntax [OK]
Hint: Use File.open with 'w' mode and a block to write safely [OK]
File opened with 'r' (read) mode but code tries to write with puts, which is not allowed.
Step 2: Confirm other options
File is closed properly; block is optional; filename looks valid.
Final Answer:
File opened in read mode but trying to write -> Option A
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
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'.
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.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.