home PYTHONJAVA
 

Ruby File input and output

Ruby provides a complete set of I/O related methods, implemented in the Kernel module. All I/O methods are derived from the IO class.

Class IO provides all the basic methods, such as read, write, gets, puts, readline, getc, and printf.

This section will cover the basic I/O functions available in all Ruby. For more functions, check out Ruby's IO class.

puts statement

In the previous section, you assigned values to variables and then printed them out using the puts statement.

The

puts statement instructs the program to display the values stored in the variable. This will add a new line at the end of each line.

Instance

#!/usr/bin/ruby val1 = "This is variable one" val2= "This is variable two" puts val1 puts val2

The above example runs the output as:

This is variable one
This is Variable two

gets statement

The

gets statement can be used to get user input from a standard screen called STDIN.

Instance

The following code demonstrates how to use the gets statement. The code will prompt the user to enter a value that will be stored in the variable val and will eventually be printed on STDOUT.

Instance

#!/usr/container/ruby puts "Enter an esteem :" val= gets puts val

The above precedent runs the yield as:

Enter an esteem : 

This is Entered esteem 

This is Entered value

putc statement

Unlike the puts articulation, the puts proclamation yields the whole string to the screen, and the putc explanation can be utilized to yield one character in grouping.

Instance

The yield of the accompanying code is only the character H:

Instance

#!/usr/canister/ruby str="Hello Ruby!" putc str

The above precedent runs the yield as:

H

print statement

The

print clarification resembles the puts enunciation. The fundamental qualification is that the puts clarification hops to the accompanying line in the wake of yielding the substance, and the cursor is arranged on a comparative line while using the print explanation.

Instance

#!/usr/canister/ruby print "Hello World" print "Good Morning"

The above precedent runs the yield as:

Hello WorldGood Morning

Open and close files

As of now, you have perused and composed standard information and yield. Presently we will see how to control the real information documents.

File.new Method

You can utilize the File.new strategy to make a File object for perusing, composing, or perusing and composing. Peruse and compose authorizations rely upon the mode parameter. At last, you can utilize the File.close strategy to close the document.

Syntax

aFile = File.new( "filename", "mode ") # ... Procedure files aFile.close

File.open method

You can utilize the File.open technique to make another record object and dole out the document item to a document. Nonetheless, there is a slight contrast between the File.open and File.new techniques. The thing that matters is that the File.open technique can be related with a square, and the File.new strategy can't.

File. Open("filename ", "mode") do |aFile| # ... process the file end

The following table lists the different modes for opening files:

ModeDescription
rRead-just mode. The document pointer is set toward the start of the record. This is the default mode.
r+Read and compose mode. The record pointer is set toward the start of the document.
wWrite mode as it were. On the off chance that the record exists, rework the document. On the off chance that the document does not exist, make another record for composing.
w+Read and compose mode. In the event that the record exists, overwrite the current document. On the off chance that the document does not exist, make another record for perusing and composing.
aWrite mode as it were. On the off chance that the record exists, the document pointer is put toward the finish of the document. At the end of the day, the record is an attach mode. On the off chance that the record does not exist, make another document for composing.
a+Read and compose mode. On the off chance that the record exists, the document pointer is put toward the finish of the document. At the end of the day, the document is an add mode. In the event that the document does not exist, make another record for perusing and composing.

Read and compose files

Methods for straightforward I/O are likewise accessible for all document objects. Along these lines, gets peruses a line from standard info, aFile.gets peruses a line from the record object aFile.

However, I/O objects give extra comfort to get to strategies, which is advantageous for us.

sysread method

You can utilize the technique sysread to peruse the substance of a record. When utilizing the strategy sysread, you can open the document in either mode. For example:

The following is the information content file:

This is a basic content record fortry purpose.

Now how about we endeavor to peruse this file:

Instance

#!/usr/bin/ruby aFile= File.new("input.txt", "r") if aFile content= aFile.sysread(20) puts content else puts "Unable to open file!" end

This statement will enter the first 20 characters of the file. The file pointer will be placed at the 21st character position in the file.

syswrite method

You can use the method syswrite to write to the file. When using the method syswrite, you need to open the file in write mode. For example:

Instance

#!/usr/bin/ruby aFile= File.new("input.txt", "r+") if aFile aFile.syswrite("ABCDEF") else puts "Unable to open file!" end

This statement will write "ABCDEF" to the file.

each_byte method

This method belongs to class File. The method each_byte is a character that can iterate over each character in the string. Take a look at the code example below:

Instance

#!/usr/bin/ruby aFile= File.new("input.txt", "r+") if aFile aFile.syswrite("ABCDEF") aFile.rewind aFile.each_byte {|ch| putc ch; putc ?. } else puts "Unable to open file!" end
The

characters are passed to the variable ch one by one and then displayed on the screen as follows:






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.