Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby replacing entire file instead of appending to it

The following ruby code is replacing the entire contents of the file. How can I just append to the end of the file and keep it's existing contents intact?

File.open("db/seeds.rb", "w") do |f|
    f.write "Blog::Engine.load_seed"
end
like image 896
Hopstream Avatar asked Feb 14 '23 03:02

Hopstream


2 Answers

Use append mode ("a"):

File.open("db/seeds.rb", "a") do |f|

Here is a link to the docs, on the different modes you can specify when opening a file.

like image 161
tckmn Avatar answered Mar 05 '23 05:03

tckmn


Write in append mode 'a'

File.write('db/seeds.rb', "Blog::Engine.load_seed", nil , mode: 'a')
like image 27
vidang Avatar answered Mar 05 '23 05:03

vidang