Reading and Writing to Tempfile

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

On Sep 21, 2009, at 6:35 AM, Brad wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs