Skip to content Skip to sidebar Skip to footer

How to Read a File in Ruby

Ruby Read File

Definition of Red Read File

To read any file system in Ruby we can use the Keyword new over file path along with the file proper noun, like File.new('examination.txt',"r"), here the test.txt is the name of the file and "r" indicates the mode in which we are going to open the file, hither "r" means we are opening the file in the read mode which means we are reading the file, in ruddy, there are many of import methods for dealing with the file read operations and all these methods are derived from the I/O classes, there are some important file reading operations like read(read the file), puts(read the variable and print the value assigned to this variable), gets, prince, print (print is similar to the puts) all of this method allow to read files.

How to Read File in Ruby-red Using Various Methods

In that location are many ways to read any file system in Ruby using various methods available in Ruddy. Allow the states hash out the methods with some examples.

1. Using New Keyword

In the below example we are opening a file with the help of the keyword telephone call new. In new we are opening the file with the read mode similar File.new("file path along with the file proper noun","r").

  • Offset, we created a file with the proper noun txt and added some contents to the file.
  • We used new with two arguments to it, in the kickoff statement nosotros are passing the name and the path of the file which nosotros are going to read and the second the style by which we are opening the file (hither in this instance we are opening the file in the read(r) way).
  • 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 assist of method spread with taking twenty as the length of the words.
  • In the output, it is printing the twenty characters from the file.

Note: Make not that here my text file test.txt is available on the same path where Ruby lawmaking file is present, In my case both on the same directories considering of that I did not crave to mention the path of the file, but in case if you have your file in the other directory and then the Cherry-red code then please mention the path instead of writing simply file proper noun. Delight follow the below code forth with the out of the screen.

Code:

simpleFile = File.new("examination.txt", "r")
if simpleFile
data = simpleFile.sysread(twenty)
puts data
else
puts "Not able to admission the file"
stop

Output:

Ruby Read File-1.1

2. Using the Keyword Open and Read

In the below example we are using a elementary open keyword to read file contents. We tin can explicate the beneath code in the post-obit steps.

  • Outset, we have written File. open up, hither File is a subclass that contains all file related activity.
  • Side by side, nosotros have written the open on File, here open will take file path as the argument .retrieve here i have passed the file name direct because of the file test.txt and file.rb both are on the same location. In case if the file is located on other location and so nosotros can pass the full path for the file.
  • After opening the file we are using the read method on the output of the result of the open.
  • Finally, we put the outcome coming from the readData.read, which is the content of the file.

Code:

readData = File.open("test.txt")
file_data = readData.read
puts file_data

Output:

Output-1.2

3. Using Read Keyword to Split Files

In the below example nosotros are using a divide keyword to read file contents. We can explain the beneath code in the following steps.

  • Beginning, nosotros take written File. read, here File is a subclass that contains all file-related activeness.
  • Side by side, we have written the split equally the concatenation grade on the output of the control File.read as the File.read.split.
  • Here File.read will take the file path as the statement. Recollect hither I have passed the file name directly because of the file examination.txt and file.rb both are in the same location. In example if a file is located in some other location then we can pass the total path for the file.
  • The split method will split each give-and-take of the sentence and read line past line.
  • Yous tin see the output of the outcome, where each word is displayed every bit the output from the sentence.

Code:

readData = File.read("examination.txt").separate
puts readData

Output:

Output-1.3

4. Using Read Lines for Reading File

Here nosotros are writing a simple code to impress each line of the file because in the below case nosotros have taken only 1 line of the sentence so that the offset line has been printed.

Code:

File.foreach("test.txt") { |each_line| puts each_line }

Output:

Output-1.4

5. Using IO for Looping Over File to Read

In this example, we are simply performing the looping on the test.txt contents line by line. The goal of the program is to print the file contents, here contents are of multiple lines. In the below instance we take taken two lines, We can explain the below example in the post-obit steps.

  • The file test.txt contains 2 lines of contents.
  • We have written a cake statement to print the code line by line.each fourth dimension content variable inside the block contains each line of the sentence.

Lawmaking:

IO.foreach("examination.txt"){|content| puts content}

Output:

Ruby Read File-1.5

Recommended Articles

This is a guide to Cerise Read File. Here we likewise discuss the Introduction and how to read file in ruby using diverse methods along with examples and its lawmaking implementation. You may likewise accept a look at the following articles to learn more –

  1. Red Information Types
  2. Crimson Modules
  3. Ruby Array Methods
  4. Install Ruby

bergerphent1973.blogspot.com

Source: https://www.educba.com/ruby-read-file/

Post a Comment for "How to Read a File in Ruby"