Close
Close

Files I/O in Ruby


When a program runs, the data is in memory but when it ends or computer shuts down, it gets lost. To keep data permanently, we need to write it in a file. Like a book, we also need to first open the file and then read or write and at last close it. Now let's see how to read or write to a file.

But before going to deal with files, let's learn something new.

ARGV


We run a Ruby code by ruby filename.rb
Now, imagine that you have written a code to calculate the square of a number. You can simply ask for the number in your program but there is also another way. You can also pass the number of which the square is to be calculated directly with the command ruby filename.rb as ruby filename.rb 2 ( passed 2 as the number ) by using ARGV. Let's look at an example to see how to write this, read the comments in the codes.

#ruby filename.rb 2
#ARGV.first will take the first passed argument i.e., 2
number = ARGV.first
#ARGV takes the arguments in form of string, so converted to integer
puts "#{number.to_i**2}"
Output
4

Read the comments in the code to understand this. We can also pass more than 1 argument to ARGV. Every passed argument will be stored as an array of string. Let's see an example for calculating the power of a number raised to another i.e. ab.

#ruby filename.rb 4 3
#ARGV will give array of string of passed arguments i.e., ["4", "3"]
a = ARGV

puts "#{a}"

b = a[0].to_i
c = a[1].to_i

puts "#{b**c}"
Output
["4", "3"]
64

As mentioned in the comments, ARGV stores the passed arguments as an array of string. You can understand rest of the code yourself.

Now, let's learn to read and write to a file.

Dealing with files in Ruby.


We open a file using open and pass the name of the file to it.

# I have a file with name test.txt in my working directory.
# In test.txt, I have, This is a test file.
f = open("test.txt")
print f.read
Output
This is a test file

We can open a file in different modes like- write mode, read mode or append mode. We use 'w' for write mode, 'r' for read mode and 'a' for append mode. You will learn all these one by one.

Writing to a file


To write something on a file, we need to open it in write mode. We write to a file by using write The example given below will make the concepts clearer.

f = open("test.txt",'w')
f.write("Hey, new text in file test.txt")
#closing the file
f.close
#opening in read mode
f = open("test.txt",'r')
print f.read
#printing a new line
print "\n"
Output
Hey, new text in file test.txt

Notice that the previous text is deleted from the file 'test.txt'. This means that opening a file in write mode starts writing from the beginning of the file. To avoid this or to write from the end of the previous text in a file, we open it in append mode ( by using 'a' ).

f = open("test.txt",'a')
f.write("Text after previous text")
#closing the file
f.close
#opening in read mode
f = open("test.txt",'r')
print f.read
#printing a new line
print "\n"
Output
Hey, new text in file test.txtText after previous text

In this example, the new text was written after the end of the previous text.

Ask Yours
Post Yours
Doubt? Ask question