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 :
So, we have created the file, myfirstfile.txt. Now, let us write the below paragraph to the file.
Ruby provides a few ways by which we can write to a file :
Let us understand puts() first using File.new() method.
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()
Now, if you open the File, myfirstfile.txt, you can find the below contents,
myfile = File.new("myfirstfile.txt", "w")
para = "In a huge pond, there lived many fish. They were arrogant and never listened to anyone."
puts para
myfile.close()
Now, let us say, we want to add three more lines to the above paragraph.
myfile = File.new("myfirstfile.txt", "a") para = "In this pond, there also lived a kind-hearted crocodile." myfile.puts para myfile.close()
Now, if you open the File, myfirstfile.txt, you can find the below contents,
myfile = File.new("myfirstfile.txt", "a")
para = "In this pond, there also lived a kind-hearted crocodile."
myfile.puts para
myfile.close()
Next, we will see, how can we read the above file in Ruby.
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
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
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()
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()
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()