home PYTHONJAVA
 

Ruby File input and output

Ruby gives a total arrangement of I/O related strategies, executed in the Kernel module. All I/O techniques are gotten from the IO class.

Class IO gives all the essential techniques, for example, read, compose, gets, puts, readline, getc, and printf.

This area will cover the essential I/O capacities accessible in all Ruby. For more capacities, look at Ruby's IO class.

puts statement

In the past segment, you doled out qualities to factors and after that printed them out utilizing the puts articulation.

The

puts articulation educates the program to show the qualities stored in the variable. This will include another line toward the finish of each line.

Instance

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

The above precedent runs the yield as:

This is variable one 

This is Variable two

gets statement

The

gets explanation can be utilized to get client contribution from a standard screen called STDIN.

Instance

The following code exhibits how to utilize the gets articulation. The code will incite the client to enter an esteem that will be put away in the variable val and will in the long run be imprinted 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.