Updated March 28, 2023
Introduction to Ruby File
Ruby file provides a beautiful way to deal with the file systems; it has many I/O methods which are used in the kernel modules; in ruby, there are many important methods for dealing with the file system, and all these methods are derived from the I/O classes, there are some important file operations like read(read the file), write(write on the file), puts(read the variable and print the value assigned to this variable), gets, princ, print (print is similar to the puts, the only difference is it will not start from the new line it cursor will be on the same line)and readline which are very common for the file system.
Various File Operations
There are various operations in Ruby on the file systems, suppose you do not know what are the operations which you can perform in the Ruby file system still you can generalize the important operations which you perform in your day to day life on the file systems; for example, you open the file, you update or write something on the file, and many more operations which you perform on a daily basis will be covered in the Ruby file system.
Examples of Ruby File
Let us start understanding the operations with some examples.
Example #1 – Opening the File for the Read-Only
Opening the file for the read-only, here we are opening the file for the read purpose and reading the content from the file. We can explain the below example in the following steps.
- First, we created a file with the name test.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 read(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 sysread with taking 20 as the length of the words.
- In the output, it is printing the 20 characters from the file.
Please follow the below code along with the out of the screen.
Code:
simpleFile = File.new("test.txt", "r")
if simpleFile
data = simpleFile.sysread(20)
puts data
else
puts "Not able to access the file"
end
Output:
Example #2 – Opening the File for the Write Mode
Opening the file for the write mode, here we are opening the file for the writing purpose and writing the content on the file. Therefore, we can explain the below example in the following steps.
- First, we created a file with the name test.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.
Code:
simpleFile = File.new("test.txt", "r+")
if simpleFile
data = simpleFile.syswrite("Yes, we should avoid the rush in current situations")
puts data
else
puts "Not able to access the file"
end
Output:
Example #3 – Renaming a File
This is a very simple example where we are renaming a file from test.txt to the final.txt. Once we execute the code given below, the output will change the test.txt to final.txt. I can explain the output screen in the below steps.
- I created a file test.txt and file.rb(contains the ruby code)
- We checked the file names with the ls command.
- Finally, we run the command ruby file.rb, which executes the code and changes the name of the file.
Please follow the below code along with the output screen.
Code:
File.rename( "test.txt", "final.txt" )
Output:
Example #4 – Deleting a File
This is a very simple example where we are deleting a file name final.txt. Once we execute the code given below, the output will delete the final.txt file. I can explain the output screen in the below steps.
- I created a file test.txt and file.rb(contains the ruby code)
- We checked the file final.txt with the ls command.
- Finally, we run the command ruby file.rb, which executes the code and it will delete the file.
- Again we checked the folder with the ls command, and we found folder does not contain the final.txt
Please follow the below code along with the output screen.
Code:
File.delete("final.txt")
Output:
Example #5 – IO based operations
In this example, we are simply performing the looping on the test.txt contents line by line. Therefore, we can explain the below example in the following steps.
- The file test.txt contains two lines of content.
- On running the command ruby file.rb will print content line by line.
Please follow the below example of code and screen of output.
Code:
IO.foreach("test.txt"){|content| puts content}
Output:
Conclusion
From these tutorials, we learned the file systems and its basic concept; we learned some of the most important and mostly used operations like reading, writing, deleting, and renaming the files; we learned about I/O operation also in the Ruby file system.
Recommended Articles
We hope that this EDUCBA information on “Ruby File” was beneficial to you. You can view EDUCBA’s recommended articles for more information.