What is the write function in Ruby?

What it does

The write function is a method in Ruby that allows you to store data inside a file.

Syntax

Following is how you use the write method:

In the syntax below, my_file represents the file pointer of the opened file.

Prerequisite for usage

To use this method, you must first open up a file using another ruby method called open. Unlike in read, we also need to pass the w flag to the open method, which stands for the write mode. Then we call the write method on the file pointer of the opened file.

Parameters

The write method only takes in the data you need to write into the file as a parameter. Besides that, you only need the file pointer to call write on.

Return value

The write method returns the total number of characters written to a file.

Examples

The following is an example where we write data to a .txt file in the same directory as our main file and then read and print the stored data:

In the example above, you might see that there is no file by the name of to_write.txt in the directory of the main file. If not present, files by the names used in the open method are generated and used at run time.

# Writing to a new file
f = File.open("to_write.txt", "w")
f.write("A very warm welcome from Educative")
f.close
# read from that same file
f = File.open("to_write.txt")
data_read = f.read
f.close
# printing the datafrom our newly written file
puts data_read

In ruby, puts is the function we use to print data out to the console similar to console.log in Nodejs

Instead of opening and closing a file to write your data every time, you can use the File.write method. The File.write method takes in the name of the file you want to open and the data you want to write in it, then writes stores that data in the file.

Following is a code example showing its usage:

# Writing to a new file
f = File.write("to_write.txt", "A very warm welcome from Educative")
# read from that same file
data_read = File.read("to_write.txt")
# printing the data from our newly written file
puts data_read

In the examples above, you write the complete file every time you use the write method. This means that all of the old data is discarded and replaced by the new data. If you wish to have the new data added to the file, you need to use the append mode in the File.write function, as shown by the following example:

# Writing 2 times to the same file without append mode
f = File.write("to_write.txt", "This is the first sentence. ")
f = File.write("to_write.txt", "This is the second sentence.\n")
# read from that same file
data_read = File.read("to_write.txt")
# printing the data from our newly written file
puts "Without the append mode: ",data_read
# writing 2 times to a new file with append mode
f = File.write("to_write_again.txt", "This is the first sentence. ", mode: "a")
f = File.write("to_write_again.txt", "This is the second sentence. ", mode: "a")
# read from that same file
data_read = File.read("to_write_again.txt")
# printing the data from our newly written file
puts "With the append mode: ",data_read

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved