How to update a file?

Hi there

Does anyone know how to write a string at the end of a file?

Cheyne Li wrote:

Hi there

Does anyone know how to write a string at the end of a file?

contents = IO.read(“data.txt”)
print contents
puts

File.open(“data.txt”, “a”) do |file|
file.print(“this is the last line\n”)
end

new_contents = IO.read(“data.txt”)
print new_contents

–output:–
hello
world
goodbye

hello
world
goodbye
this is the last line

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs