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




RUBY - WRITE TO FILE


So in the previous tutorial, we have created file myfirstfile.txt using the File.new() method.


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

Now, let us see, how can we write something to the file.


There are two modes of writing to a File :

  1. "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.

  2. "w+" - Create Mode - Create mode is used to create a new file. But if the file is present,it throws an error.

  3. "a" - Append 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.

  4. "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.

So, we have created the file, myfirstfile.txt. Now, let us write the below paragraph to the file.

In a huge pond,

there lived many fish.

They were arrogant and

never listened to anyone.

Ruby provides a few ways by which we can write to a file :

  1. puts

  2. write

  3. syswrite

  4. <<

Let us understand puts() first using File.new() method.


puts() method using the write mode 'w' and File.new()


Example :



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

para = "In a huge pond, 
there lived many fish.
They were arrogant and 
never listened to anyone."
	
myfile.puts para
myfile.close()


Output :




Now, if you open the File, myfirstfile.txt, you can find the below contents,

java_Collections
  1. So, what we have done in the above example is, opened the file in Write Mode(i.e. w).

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


    We haven't specified the Text Mode (i.e. t) because it is the default mode.

  2. Then we have initialise the paragraph in a variable, para using triple quotes.

    para = "In a huge pond,
    there lived many fish.
    They were arrogant and
    never listened to anyone."

  3. After that we have used the write() Method to write the paragraph to the file.

    puts para

  4. Finally, we close the file using the close() Method.

    myfile.close()

Now, let us say, we want to add three more lines to the above paragraph.

In this pond,

there also lived

a kind-hearted crocodile.

write() method using the write mode 'a' and File.new()


Example :



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

para = "In this pond, 
there also lived 
a kind-hearted crocodile."
	
myfile.puts para
myfile.close()


Output :




Now, if you open the File, myfirstfile.txt, you can find the below contents,

java_Collections
  1. So, what we have done in the above example is, opened the file in Append Mode(i.e. a).

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

  2. Then we have initialised the paragraph in a variable, para using triple quotes. So that we could append those three more lines to the existing File.

    para = "In this pond,
    there also lived
    a kind-hearted crocodile."


    We have put the escape character \n. So that the new lines are added to the next line of the existing contents of the file.

  3. After that we have used the write() Method to write the paragraph to the file.

    myfile.puts para

  4. Finally, we close the file using the close() Method.

    myfile.close()

Next, we will see, how can we read the above file in Ruby.


Write to file using '<<' and write mode 'w' and File.open()


Example :



para = "In a huge pond, 
there lived many fish.
They were arrogant and 
never listened to anyone."

para1 = "\nIn this pond, 
there also lived 
a kind-hearted crocodile."


File.open("myfirstfile4.txt", "w") do |myfile|
	myfile.syswrite para
	myfile << para1
	myfile.close()
end	


Output :




So, we have used File.open() to open a file.


Now, if you see the above example, you can see the File.open() has a block where you can write multiple statement.


File.open("myfirstfile4.txt", "w") do |myfile|
	myfile.syswrite para
	myfile << para1
	myfile.close()
end

So we have created two variables, para,


para = "In a huge pond,
there lived many fish.
They were arrogant and
never listened to anyone."

And para1,


para1 = "\nIn this pond,
there also lived
a kind-hearted crocodile."

Then inside the block of File.open(),


File.open("myfirstfile4.txt", "w") do |myfile|
	...
	...
	...
end

We have placed syswrite and <<, to write the contents of para and para1 to the file,myfirstfile4.txt.


myfile.syswrite para
myfile << para1

And finally, we have closed the file,


myfile.close()

The only thing to note is, you can place multiple file operations inside the block of File.open(.


File.open("myfirstfile4.txt", "w") do |myfile|
	...
	...
	...
end

write() method using the write mode 'w' and File.new()


Example :



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

para = "In a huge pond, 
there lived many fish.
They were arrogant and 
never listened to anyone."
	
myfile.write para
myfile.close()


Output :




syswrite() method using the write mode 'w' and File.new()


Example :



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

para = "In a huge pond, 
there lived many fish.
They were arrogant and 
never listened to anyone."
	
myfile.syswrite para
myfile.close()


Output :




Write to file using '<<' and write mode 'w' and File.new()


Example :



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

para = "In a huge pond, 
there lived many fish.
They were arrogant and 
never listened to anyone."
	
myfile << para
myfile.close()


Output :