Updated April 5, 2023
Introduction to Ruby Write to File
To perform a write operation on the file Ruby provides various methods and ways, all the operations performed on any file system will be done with the File class of the ruby. File class system allows the developer to use the various method on it like open, new and write to perform the writing work on the file system, it allows us to write either in append (add new contents on the existing content with newlines) mode or on normal write (, in Ruby, the shortest way to write the content will be done with the method), write (here write method will work in a various mode like append and write).
Write to File in Ruby Using Various Methods
We have various ways and methods available in Ruby to write the contents on the file like we can use new, we can use open in append mode and write mode, we can use the direct-write method (write is also available in a various mode like append mode and write mode). Let us discuss all these possible ways of writing on any file.
1. Using new
We can perform the write operation using the new Ruby keyword. Opening the file for the write mode, here we are opening the file for the write purpose and writing the content on the file. We can explain the below example in the following steps.
- First, we created a file with the name txt and added some contents to the file.
- We used new with two arguments to it, in the first argument we are passing the name and the path of the file which we are going to read and the second the mode by which we are opening the file (here in this example we are opening the file in the write(+r) mode) .
- The next code is an if statement that we are checking if the file is empty to avoid useless code flow for empty contents.
- Finally with the help of method syswrite, which will write the content or simply remove existing contents and add the contents passed into the function. This function will return the total length of the letter on the file. You can see in the example it returns 48.
- We can see the content of the file test.txt before executing the code and after executing the code block.
Please see the below example.
Code:
#Here argument r+ indicate we are opening the file in write mode .
simpleFile = File.new("test.txt", "r+")
if simpleFile
data = simpleFile.syswrite("Yes , we should avoid rush in current situations")
puts data
else
puts "Not able to access the file"
end
Output:
2. Using the Open in Append Mode
Below is a simple example where we are opening a file test.txt in append mode. Append mode means each time when we execute the code it will add the new content in the new line. This type of operation is performed when we need older content and new content on the file system. We can explain the below example in two important steps.
- In the first step, we are opening the file test.txt and going to each line and adding new content on the file on the new line.
- In the second step, we are opening the file in read mode and printing all the contents of the file.
Please see the below example.
Code:
#Here the argument "a" indicates we are opening the file in append mode .
File.open("test.txt","a") do |line|
line.puts "\r" + "Welcome again in file system"
end
File.open("test.txt").each do |line|
puts line
end
Output:
3. Using Open in the Write Mode
Below is a simple example where we are opening a file test.txt in write mode. Write mode means each time when we execute the code it will add the new and remove older content. This type of operations are performed when we do not need the older contents. There are two main steps which we are performing they are given below.
- In the first step, we are opening the file in write mode and adding new content by replacing the older one.
- In the second step, we are opening the file in read mode and printing all the contents of the file.
Please see the below example.
Code:
#Here argument w indicates we are opening the file in write mode .
File.open("test.txt","w") do |line|
line.puts "\r" + "Welcome again in file system"
end
File.open("test.txt").each do |line|
puts line
end
Output:
4. Using the write Method Directly
In the below example we are not opening the file, we are directly writing content on the file with the help of the method write. We are performing two main operations which can be explained in two steps.
- In the first step, we are writing the content by passing two arguments to the method write (file name and contents). We can also pass the mode of writing like append or write(in the same way of open method).
- Second, we are reading the file content which was added by the previous step.
Please see the below example.
Code:
File.write('test.txt', 'This we are adding without opening the file')
File.open("test.txt").each do |line|
puts line
end
Output:
Conclusion
From this tutorial we learned about writing on the file system, we saw various ways of writing on any file system with the help of available methods in Ruby. We learned the uses of the methods with the help of some important examples.
Recommended Article
We hope that this EDUCBA information on “Ruby Write to File” was beneficial to you. You can view EDUCBA’s recommended articles for more information.