Introduction
Ruby delivers us with a variety of methods for dealing with files. In simple terms, file handling includes numerous steps, such as creating a new file, reading the contents of a file, adding some material to a file, appending content to a file, deleting a file, and more. You can use Ruby to access various types of files, interact with the data they contain, and then probably produce a new file with your answer.
Today, you will learn how to read and write files in Ruby so that you may extract the contents, create new files, and get the information you require.
Reading a File in Ruby
In Ruby, we may read any file system by using the keyword new over the file path and the file name, such as File.new('test.txt',"r"). Here, "r" means that we are opening the file in read mode, which means that we are reading the file. In Ruby, there are many significant methods for dealing with file read operations, all of which are derived from the I/O classes. Some significant file reading operations include read (read the file), puts (read the variable and print the value assigned to that variable), and so on.
Methods for Reading a File in Ruby
There are numerous ways in Ruby to read any file system utilizing the various techniques available in Ruby. Let's look at some examples of the methods.
Using the ‘new’ Keyword
-
First, a text file is created, and some content is inserted into it
-
We used a new keyword with two arguments, the first being the name and path of the file to be read and the second being the mode in which we are opening the file (in this example, we are opening the file in the read(r) mode)
-
The next part of the code is an if statement that checks if the file is empty to save unnecessary code flow for empty contents
-
Finally, using the sysread method, take 40 as the length of the words
-
It prints the first 40 characters from the file in the output
Code:
Output:
Using Open and Read Keyword
-
First, the File.open method is used, which is a subclass that contains all file-related behavior
-
The open function takes a file path as an argument. Remember that the file name is passed directly here because the files test.txt and code1.rb are both in the same directory. If the file is at a different location, we can provide the entire path to it
-
After opening the file, use the read method on the output of the open result
-
Finally, we put the result of readData.read, which is the file's content
Code:
Output:
Using Read Keyword to Split Files
-
First, File.read is written, which is a subclass that contains all file-related behavior
-
The split was then written as a chain form on the output of the command File.read as File.read.split
-
File.read will take the file path as input here. Remember that the file name is passed directly here because the files test.txt and file.rb are both in the same directory. If a file is in a different location, we can provide the entire path of the file
-
The split method divides the language into words and reads them line by line
-
You can see the output of the result, where each word is displayed as the sentence's output
Code:
Output: