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/bin/ruby puts "Enter a value :" val= gets puts val

The above example runs the output as:

Enter a value :
This is Entered value
This is Entered value

putc statement

Unlike the puts statement, the puts statement outputs the entire string to the screen, and the putc statement can be used to output one character in sequence.

Instance

The output of the following code is just the character H:

Instance

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

The above example runs the output as:

H

print statement

The

print statement is similar to the puts statement. The only difference is that the puts statement skips to the next line after outputting the content, and the cursor is positioned on the same line when using the print statement.

Instance

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

The above example runs the output as:

Hello WorldGood Morning

Open and close files

As of now, you have read and written standard input and output. Now we will look at how to manipulate the actual data files.

File.new Method

You can use the File.new method to create an File object for reading, writing, or reading and writing. Read and write permissions depend on the mode parameter. Finally, you can use the File.close method to close the file.

Syntax

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

File.open method

You can use the File.open method to create a new file object and assign the file object to a file. However, there is a slight difference between the File.open and File.new methods. The difference is that the File.open method can be associated with a block, and the File.new method cannot.

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

The following table lists the different modes for opening files:

ModeDescription
rRead-only mode. The file pointer is placed at the beginning of the file. This is the default mode.
r+Read and write mode. The file pointer is placed at the beginning of the file.
wWrite mode only. If the file exists, rewrite the file. If the file does not exist, create a new file for writing.
w+Read and write mode. If the file exists, overwrite the existing file. If the file does not exist, create a new file for reading and writing.
aWrite mode only. If the file exists, the file pointer is placed at the end of the file. In other words, the file is an append mode. If the file does not exist, create a new file for writing.
a+Read and write mode. If the file exists, the file pointer is placed at the end of the file. In other words, the file is an append mode. If the file does not exist, create a new file for reading and writing.

Read and write files

Methods for simple I/O are also available for all file objects. So, gets reads a line from standard input, aFile.gets reads a line from the file object aFile.

However, I/O objects provide additional convenience for access methods, which is convenient for us.

sysread method

You can use the method sysread to read the contents of a file. When using the method sysread, you can open the file in either mode. For example:

The following is the input text file:

This is a simple text file fortry purpose.

Now let's try to read 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.