Forum: Ruby Writing a file, without deleting it's current content

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E17632fab00b930ea3b30e1b98f39675?d=identicon&s=25 Jeppe Jakobsen (Guest)
on 2006-04-03 00:07
(Received via mailing list)
Hi, I just read about the ".write" method.
Let's say that I've just written a string to a file, "file.txt". Then I
want
to add some more information to it, on the next line. I use .write again
and
my info got replaced. How do I add to the without deleting what's
already in
it. I know I can load the string from it and then add them together, but
isn't there a smarter way to do it, like writing to the next line?

Thanks in advance :)
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-04-03 00:16
(Received via mailing list)
Jeppe Jakobsen wrote:
> Hi, I just read about the ".write" method.
> Let's say that I've just written a string to a file, "file.txt". Then I want
> to add some more information to it, on the next line. I use .write again and
> my info got replaced. How do I add to the without deleting what's already in
> it. I know I can load the string from it and then add them together, but
> isn't there a smarter way to do it, like writing to the next line?
>
> Thanks in advance :)
>

Look up the "append" option for File when opening for writing.


--
James Britt

"Programs must be written for people to read, and only incidentally
  for machines to execute."
   - H. Abelson and G. Sussman
   (in "The Structure and Interpretation of Computer Programs)
E17632fab00b930ea3b30e1b98f39675?d=identicon&s=25 Jeppe Jakobsen (Guest)
on 2006-04-03 13:24
(Received via mailing list)
ok just to make sure I got it right:

File.open("myFile.txt.", APPEND="This is the string I'm writing" )

Correct?

2006/4/3, James Britt <james_b@neurogami.com>:
Bd0203dc8478deb969d72f52e741bd4f?d=identicon&s=25 Daniel Baird (Guest)
on 2006-04-03 13:30
(Received via mailing list)
erm, try this:

myfile = File.open("myFile.txt","a")
myfile << "This is the string I'm writing"


On 03/04/06, Jeppe Jakobsen <jeppe88@gmail.com> wrote:
> > > Hi, I just read about the ".write" method.
> > > isn't there a smarter way to do it, like writing to the next line?
> > "Programs must be written for people to read, and only incidentally
>
--
Daniel Baird
http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things
That Suck)
[[My webhost uptime is ~ 92%.. if no answer pls call again later!]]
This topic is locked and can not be replied to.