File Input and Output in Ruby

RMAG news

As we know that Ruby one of the language for backend technology, so it must have a module to deal with file system in your Operating system.

Ruby I/O is a way to interact with your system. Data is sent in the form of bytes/characters. IO class is the basis for all input and output in Ruby. It may be duplexed, hence may use more than one native operating system stream.

IO has a subclass as File class which allows reading and writing files in Ruby. The two classes are closely associated. IO object represent readable/writable interactions to keyboards and screens.

Common modes in I/O port

As it is known in most of other languages that have file system module, they have modes when dealing with file.

like if you want to append, delete, add, or read and this modes in ruby when you dealing with files:

“r”: read-only mode is the default mode starts at beginning of file.
“r+”: read-write mode, starts at beginning of file.
“w”: write-only mode, either creates a new file or truncates an existing file for writing.
“w+”: read-write mode, either creates a new file or truncates an existing file for reading and writing.
“a”: write-only mode, if file exists it will append the file otherwise a new file will be created for writing only.
“a+”: read and write mode, if file exists it will append the file otherwise a new file will be created for writing and reading.- “r”: read-only mode is the default mode starts at beginning of file.
“r+”: read-write mode, starts at beginning of file.
“w”: write-only mode, either creates a new file or truncates an existing file for writing.
“w+”: read-write mode, either creates a new file or truncates an existing file for reading and writing.
“a”: write-only mode, if file exists it will append the file otherwise a new file will be created for writing only.
“a+”: read and write mode, if file exists it will append the file otherwise a new file will be created for writing and reading.

Ruby opening a file

A Ruby file can be created using different methods for reading, writing or both.

f = File.new(“fileName.rb”)

or

File.open(“fileName.rb”, “mode”) do |f|

The different between open and new methods that open can take block of code and determine a specific mode.

Ruby reading a file

There are three different methods to read a file.

To return a single line, following syntax is used.

while line = gets
puts line
end

To return the whole file after the current position, following syntax is used.

f.read

To return file as an array of lines, following syntax is used.

f.readlines

Ruby writing a file

#!/usr/bin/ruby

aFile = File.new(“about.txt”, “r+”)
if aFile
aFile.syswrite(“New content is written in this file.n)
end

Ruby renaming and deleting a file

Ruby files are renamed using rename method and deleted using delete mehtod.

To rename a file, following syntax is used.

File.rename(“old.txt”, “new.txt”)

To delete a file, following syntax is used.

File.delete(“filename.txt”)

Leave a Reply

Your email address will not be published. Required fields are marked *