Ruby Tutorial
Ruby CGI
Ruby Advanced
File Input/Output (I/O) is an integral part of any programming language, and Ruby is no exception. Ruby makes it easy to work with files, allowing you to read from and write to them with ease. Here's a tutorial on Ruby file I/O:
1. Creating a New File:
You can create a new file using the File.new
method, which also opens the file:
file = File.new("test.txt", "w") file.close
In this example, "test.txt" is the filename and "w" is the mode (write). The file.close
statement is used to close the file after we're done with it.
2. Opening and Reading from a File:
The File.open
method can be used to open a file. If you provide a block, the file will automatically be closed when the block finishes:
File.open("test.txt", "r") do |file| while line = file.gets puts line end end
In this example, "test.txt" is the filename and "r" is the mode (read). The file.gets
statement is used to read the file line by line.
3. Writing to a File:
You can write to a file using the write
method:
File.open("test.txt", "w") do |file| file.write "Hello, world!" end
In this example, "test.txt" is the filename and "w" is the mode (write). The file.write
statement is used to write to the file.
4. Appending to a File:
You can append to a file using the "a" mode:
File.open("test.txt", "a") do |file| file.puts "Hello again, world!" end
In this example, "test.txt" is the filename and "a" is the mode (append). The file.puts
statement is used to append a line to the file.
5. Reading and Writing at the Same Time:
You can open a file for both reading and writing using the "r+" mode:
File.open("test.txt", "r+") do |file| file.puts "Hello, world!" file.seek(0) # Go back to the start of the file puts file.gets # Read the first line of the file end
In this example, "test.txt" is the filename and "r+" is the mode (read/write). The file.seek
method is used to change our position in the file, and file.gets
is used to read from the file.
Remember that working with files is a common source of errors in programming, because many things can go wrong. The file might not exist, you might not have permission to access it, or it might be locked by another process. Always make sure to handle these potential errors in your code to make it robust and reliable.
Reading from a file in Ruby:
File.open('filename.txt', 'r') do |file| content = file.read puts content end
Writing to a file in Ruby:
File.open('filename.txt', 'w') do |file| file.puts 'Hello, world!' end
File open and close in Ruby:
file = File.open('filename.txt', 'r') # Do something with the file file.close
Appending to a file in Ruby:
File.open('filename.txt', 'a') do |file| file.puts 'Appending new content' end
Ruby File readlines method:
lines = File.readlines('filename.txt') puts lines
Ruby File each_line iterator:
File.open('filename.txt', 'r') do |file| file.each_line do |line| puts line end end
Reading and writing binary files in Ruby:
File.open('binaryfile.bin', 'wb') do |file| file.write(binary_data) end binary_data = File.open('binaryfile.bin', 'rb') { |file| file.read }
Ruby File I/O error handling:
begin File.open('nonexistent.txt', 'r') do |file| # Code to read from the file end rescue Errno::ENOENT puts 'File not found!' end
Working with CSV files in Ruby:
require 'csv' CSV.open('data.csv', 'w') do |csv| csv << ['Name', 'Age'] csv << ['John', 30] end
File locking in Ruby:
File.open('file.txt', 'a') do |file| file.flock(File::LOCK_EX) # Exclusive lock file.puts 'Appending with lock' file.flock(File::LOCK_UN) # Release lock end
Ruby STDOUT and STDERR:
puts 'This goes to STDOUT' $stderr.puts 'This goes to STDERR'
Redirecting output in Ruby:
$stdout = File.new('output.txt', 'w') puts 'This goes to output.txt' $stdout = STDOUT # Reset to default