Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - FILES


We all have came across handling files in some way. Be it a .txt file or a .dat or even a .py file that we use to write our Ruby Programmes.


So, files are something, where we store some information.


Say for example, we want to write the below paragraph to a file.

In a huge pond,

there lived many fish.

They were arrogant and

never listened to anyone.

So, we want to store the four lines in a File and name the File as myfirstfile.txt.


And in general, you need to follow the below steps create the file and write the lines :

  1. You need to open the File in a Notepad or TextEdit and name the file as myfirstfile.txt.

  2. Then you need to write the above four lines.

  3. And close the file once done.

Again, if you want to make some changes to the file(Say adding or deleting a few lines) :

  1. You need to open the existing File myfirstfile.txt in a Notepad or TextEdit.

  2. Make some changes to it.

  3. Close the file.

Similarly, if you want to delete the myfirstfile.txt :

  1. You go to that file and delete it.

Now, let us corelate, how can we achieve the same in Ruby.


The first thing is to open a file. And to open a file in Ruby, there is a Method called File.new() that accepts two Arguments. They are the name of the file (i.e. myfirstfile.txt) and Mode.


A Mode states, what you want to do with the file, once you open it. i.e. You can open a file to read it, to write something to it, to append it and create the file the first time you try opening it.


Below are the file Modes :

  1. "r" - Read Mode - This is the default mode. Used to opens a file for reading and throws an error if the file is not present.

  2. "r+" - Read and Write Mode - Read and Write mode is used to write something from the beginning of the file. And if the file is not present, throws an error.

  3. "w" - Write Mode - Write mode is used to write something to the file. It overwrites the contents of the file. And if the file is not present, it creates a new file.

  4. "w+" - Read and Write Mode - Read and Write Mode is used to read and write something to the file. It overwrites the contents of the file. And if the file is not present, it creates a new file.

  5. "a" - Append(Write mode) Mode - Append mode is used to write something at the end of the file. And if the file is not present, it creates a new file.

  6. "a+" - Append(Read Write) Mode - Append mode is used to read and write something at the end of the file. And if the file is not present, it creates a new file.

There are two ways by which we can create and open a file:

  1. File.new() method

  2. File.open() method

Let us look at the below example to create a File named myfirstfile.txt using File.new().


Example :



myfile = File.new("myfirstfile.txt", "w")



Now, if you search the above file myfirstfile.txt in the current directory. You can find an empty file myfirstfile.txt in the current folder.


So, in the above example, we have created a blank file called named myfirstfile.txt using the File.new() Method.


myfile = File.new("myfirstfile.txt", "w")

The Method, File.new() has two Arguments. First argument is the File name (i.e. myfirstfile.txt). And the second Argument is the Mode(i.e. w). Where w is the write mode.


Now, let us look at the below example to create a File named myfirstfile.txt using File.open().


Example :



File.open("myfirstfile.rb", "w") do |myfile|



Next, let us see, how can we write the above lines to the file myfirstfile.txt.