Reading and Writing to Tempfile

B

Brad

Hello,

I'm having a hard time getting tempfile to work correctly for me. I
have a couple of questions

Isn't it possible to open a tempfile, write to it, close it then later
open it again write to it and close it?
I don't seem to be able to do that. Every time I write to the file it
only or over writes what is in the file.

require 'tempfile'

temp = Tempfile.new("stuff")
name = temp.path
temp.puts "I was here"
temp.close

# here I'd like to reopen the file and write another line

#later
temp.open
str = temp.gets # I was here

temp.close(true)

What am I doing wrong?

Any help is good, thank you.

Tex
 
J

Joshua Ballanco

Hello,

I'm having a hard time getting tempfile to work correctly for me. I
have a couple of questions

Isn't it possible to open a tempfile, write to it, close it then later
open it again write to it and close it?

Check the documentation on IO (I know, I know...the class is "File"
but all the important documentation is under "IO"). Specifically, you
want to set the mode on the file to "a" instead of just "w" when you
open it.

- Josh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top