Ruby Tutorial
Ruby CGI
Ruby Advanced
The File
class in Ruby is used for handling and manipulating files. Here are some of the most commonly used methods:
1. Creating a New File:
You can create a new file using the 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). Remember to close the file afterwards with file.close
.
2. Opening a File:
The open
method can be used to open a file. It also creates a new file if it doesn't exist:
File.open("test.txt", "w") do |file| file.puts "Hello, world!" end
Here, "test.txt" is the filename and "w" is the mode (write). The block is executed with the file object as the parameter, and the file is automatically closed after the block is executed.
3. Reading from a File:
You can read the contents of a file using the read
method:
content = File.read("test.txt") puts content
4. 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
5. 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
6. Checking if a File Exists:
The exist?
method can be used to check if a file exists:
puts File.exist?("test.txt") # Outputs: true or false
7. Deleting a File:
The delete
method can be used to delete a file:
File.delete("test.txt")
8. Renaming a File:
The rename
method can be used to rename a file:
File.rename("oldname.txt", "newname.txt")
9. Getting File Size:
The size
method can be used to get the size of a file in bytes:
puts File.size("test.txt")
10. Getting File Information:
The stat
method returns a File::Stat
object, which contains information about the file:
info = File.stat("test.txt") puts info.size # File size in bytes puts info.atime # Last access time puts info.mtime # Last modification time
These are just a few of the most commonly used methods in the File
class. There are many more methods for more advanced operations, such as changing file permissions or reading file metadata. For a complete list, refer to the Ruby documentation.
Ruby File open method:
File.open
method is used to open or create a file, providing a file handle for subsequent operations.file = File.open('example.txt', 'w') # Perform file operations file.close
Read and write files in Ruby:
File.read
and File.write
for simple file read and write operations.content = File.read('example.txt') File.write('output.txt', content)
Working with file paths in Ruby:
File.join
to safely concatenate file paths.file_path = File.join('path', 'to', 'file.txt')
Ruby File read method example:
File.read
method reads the entire content of a file.content = File.read('example.txt') puts content
Ruby File write method example:
File.write
method writes content to a file, overwriting existing content.File.write('example.txt', 'Hello, Ruby!')
File modes in Ruby File class:
'r'
, 'w'
, and 'a'
, determine how files are opened (read, write, append).file = File.open('example.txt', 'r')
Checking if a file exists in Ruby:
File.exist?
or File.file?
to check if a file exists.if File.exist?('example.txt') puts 'File exists.' else puts 'File does not exist.' end
File permissions in Ruby:
File.chmod
method.File.chmod(0644, 'example.txt') # Sets read and write permissions for owner
Appending to a file in Ruby:
'a'
mode to open a file for appending.File.open('example.txt', 'a') { |f| f.puts 'Appended content' }
Deleting a file with Ruby File class:
File.delete
method deletes a file.File.delete('example.txt')
Listing files in a directory with Ruby:
Dir.entries
or Dir.glob
to list files in a directory.files = Dir.entries('.') puts files
Ruby File size and properties:
File.size
to get the size of a file and File.mtime
for the last modification time.size = File.size('example.txt') mtime = File.mtime('example.txt')