Complete the code to open a file named 'example.txt' for reading.
File.[1]('example.txt') do |file| puts file.read end
The File.open method opens the file and allows you to work with it inside the block.
Complete the code to open 'data.txt' for writing inside a block.
File.open('data.txt', [1]) do |file| file.puts 'Hello, world!' end
The mode 'w' opens the file for writing, creating or truncating it.
Fix the error in the code to properly open and auto-close 'log.txt' for appending.
File.open('log.txt', [1]) do |file| file.puts 'New entry' end
The mode 'a' opens the file for appending, adding content at the end.
Fill both blanks to open 'notes.txt' for reading and print its content line by line.
File.open('notes.txt', [1]) do |file| file.[2] do |line| puts line end end
Open the file in read mode 'r' and use each_line to iterate over each line.
Fill all three blanks to open 'output.txt' for writing, write a line, and automatically close the file.
File.[1]('output.txt', [2]) do |file| file.[3] 'This is a test.' end
Use File.open with mode 'w' to write, and puts to write a line.