Ruby binary temp file

W

wimxa

require 'tempfile'
f = Tempfile.open 'tmp'

will open f in text mode (mode = 'w', not 'wb'). Any non-hacky way to
open this in binary mode? I can, of course, make f, close it and open
it as a real file - it's just this is not atomic.
 
D

Daniel Berger

require 'tempfile'
f = Tempfile.open 'tmp'

will open f in text mode (mode = 'w', not 'wb'). Any non-hacky way to
open this in binary mode? I can, of course, make f, close it and open
it as a real file - it's just this is not atomic.

Not that I can see.

May I suggest file-temp? It opens in binary mode by default:

http://raa.ruby-lang.org/project/file-temp

Regards,

Dan
 
J

James Edward Gray II

require 'tempfile'
f = Tempfile.open 'tmp'

will open f in text mode (mode = 'w', not 'wb'). Any non-hacky way to
open this in binary mode? I can, of course, make f, close it and open
it as a real file - it's just this is not atomic.

You can switch it to binary mode after it's open:

#!/usr/bin/env ruby -wKU

require "tempfile"

f = Tempfile.new "binary"
f.binmode # switch to binary mode

__END__

James Edward Gray II
 

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
474,269
Messages
2,571,346
Members
48,021
Latest member
anchal126

Latest Threads

Top