- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Read and Writes Files in Ruby
Ruby provides us with different methods that we can use to handle files. In simple terms, file handling involves different processes such as creating a new file, reading the contents present in a file, writing some content to a file, appending content to a file, deleting a file and more.
There are different modes that one can use to do file handling in Ruby. These are −
r = Read-only mode
r+ = Read-Write mode
w = Write-mode only
w+ = Read-write mode
And some more. These four mentioned modes are the most commonly used ones, when it comes to file handling in Ruby. In this article, we will explore all the major cases of file handling one by one with the help of examples.
Creating a File in Ruby
Let's start with the most common and useful one, how to create a file in Ruby. Consider the code shown below.
# Creating a file fileObj = File.new("tutorials.txt", "w+"); # Writing to the file fileObj.syswrite("This file contains knowledge!"); # Closing a file fileObj.close();
When we want to create a new file in Ruby, we use the new() method of the File module, and it takes two arguments. The first argument is the name of the file and the second argument is the mode in which we want to open the file.
If we write the following code on any Ruby IDE, then a new file will be created in the same directory. We can confirm the same by opening the file using the cat command.
Command −
cat tutorials.txt
It will produce the following output.
This file contains knowledge!
Opening a File in Ruby
Now we will see how to open a file in Ruby. There are multiple methods that we can use for opening a file in Ruby. There are two methods which are widely used − the sysread(n) and the read() method.
The open method is used to open the file, while the sysread(n) is used to read the first "n" characters from a file, and the read() method is used to read the entire file content.
Example
Consider the code shown below.
fileObject = File.open("tutorials.txt","r"); puts(fileObject.sysread(10)); fileObject.close();
In the above code, we are reading the first 10 characters of the file named "tutorials.txt".
It will produce the following output.
This file
Here is the another example of opening a file in Ruby.
fileObject = File.open("tutorials.txt","r"); print(fileObject.read()); fileObject.close();
It will produce the following output.
This file contains knowledge!
Now let's try to rename a file in Ruby.
Renaming a File in Ruby
To rename a file in Ruby, we need to use the rename method that is present in the File module.
puts File.rename("tutorials.txt", "sample.txt")
This command will change the name of the file from "tutorials.txt" to "sample.txt". And, it will display the following output on the console −
0
Deleting a File in Ruby
Last but not the least, let's consider how we can delete a file in Ruby. To delete a file, we just need to call the delete method present in the File module.
puts File.delete("sample.txt")
If we write the following code on any Ruby IDE, then we will get the following output on the console −
1
- Related Articles
- How to read and parse CSV files in C++?
- How to read CSV files in Golang?
- How to read and write unicode (UTF-8) files in Python?
- How to read text files using LINECACHE in Python
- How to read files from assets on Android using Kotlin?
- How to read data from all files in a directory using Java?
- How to read multiple text files from a folder in Python?(Tkinter)
- Read and write WAV files using Python (wave)
- How to read all files in a folder to a single file using Java?
- Golang program to read and print all files from zip file
- Read and write AIFF and AIFC files using Python (aifc)
- Read and write tar archive files using Python (tarfile)
- How to use BigDecimal in Ruby?
- How to freeze objects in Ruby?
- How to Handle Exceptions in Ruby
