battleship
battleship

Reputation: 353

clean ruby file before writing new content to it

Can somebody please guide me a way to clean all existing content in a file before writing new info to it in Ruby? I'm writing the contents of this file with the code below:

logfile = File.new(filepath, "w")
logfile.write("my content")

However, I want all existing content in this "logfile" to be deleted before I write new info to it. How can I do this?

Upvotes: 6

Views: 5128

Answers (1)

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70949

When opening a file for writing, i.e. using the "w" option, all previous contents of the file will be removed so you don't have to do anything explicitly to achieve that effect.

Upvotes: 13

Related Questions