Forum: Ruby append new line to textfile

Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2008-11-21 22:45
I want to create a text file and append a new line to the text file
everytime a new logline is completed, my program iterates and gives a
new log every few hours.

this is an example of my logline:
logline: 'completed' starttime 04:00 endtime 23:00

my goal is to have a text file populated with the following
'completed' starttime 04:00 endtime 22:00
'completed' starttime 06:00 endtime 01:00
'completed' starttime 05:00 endtime 23:00
'completed' starttime 09:00 endtime 23:00

so far this is my method...yes, I am a beginner. Thanks in advance. MC

File.open('logfile.txt')do |f1|
f1.logline
end
12a71a456ac3d464914a8267f11d43b3?d=identicon&s=25 Shane Emmons (Guest)
on 2008-11-21 23:03
(Received via mailing list)
On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom
<mmc_collins@yahoo.com>wrote:

> 'completed' starttime 05:00 endtime 23:00
>
File.open('logfile.txt', 'w+') do |f1|  f1.write(logline)
end
D7908f05c89e965f6bc5308ad6f41256?d=identicon&s=25 Siep Korteling (steenslag)
on 2008-11-21 23:27
Shane Emmons wrote:
> On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom
> <mmc_collins@yahoo.com>wrote:
>
>> 'completed' starttime 05:00 endtime 23:00
>>
> File.open('logfile.txt', 'w+') do |f1|  f1.write(logline)
> end
This will destroy the content of logfile.txt. For appending to a file
instead of overwriting it, use "a"

3.times do |n|
  logline = "logline #{n}"
  File.open('D:/temp/logfile1.txt', 'w+') do |f1|
    f1.puts(logline)
  end
  File.open('D:/temp/logfile2.txt', 'a') do |f1|
    f1.puts(logline)
  end
end

hth,

Siep
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-11-21 23:28
(Received via mailing list)
On Nov 21, 2008, at 4:58 PM, Shane Emmons wrote:
>> 'completed' starttime 04:00 endtime 22:00
>>
> File.open('logfile.txt', 'w+') do |f1|  f1.write(logline)
> end
> --
> Shane Emmons


Eek! No, that's for a read/write file.  You want a mode of 'a' for
append.  All write operations will first reposition to the end-of-file.

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
12a71a456ac3d464914a8267f11d43b3?d=identicon&s=25 Shane Emmons (Guest)
on 2008-11-21 23:37
(Received via mailing list)
On Fri, Nov 21, 2008 at 5:23 PM, Siep Korteling
<s.korteling@gmail.com>wrote:

>
> hth,
>
> Siep
> --
> Posted via http://www.ruby-forum.com/.
>
>
whoops, sorry about that.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.