Ruby: Add line in file -


this question has answer here:

i have file in 1 want store datas.

using irb, can add different lines in file. however, using ruby script writen in file, have issues.

i can write line, stored should be, when re launch script , re use method, overwrites in file instead of adding content @ next line.

def create_new_account     puts "set account's name"     @account_name = gets     puts "new account's name: #{@account_name}     open("accounts.txt","w+") |account_file|         account_file.write "ac;#{@account_name}\n"      end end 

i had different parameters of method open, seems it's not there. moreover, tried puts instead of write, there no difference, same problem.

could me understand wrong code?

thanks

try opening file in append mode so

open('accounts.txt', 'a+') 

otherwise file opened overwrite existing data.

"a" - write-only, each write call appends data @ end of file. creates new file writing if file not exist.


Comments