File#truncate fills file with zeros

L

Leslie Viljoen

Hello!

Running this following code:

f = File.open(filename, 'w')
f.truncate(10)
f.close

leaves the file filled with 10 zero bytes. Its pretty unexpected,
since the documentation makes no mention of this
(http://ruby-doc.org/core/classes/File.html#M002539).

File.truncate(filename, 10)

works fine, leaving any pre-existing bytes intact. Looking at the
example I assume this has to do with the File#truncate method
expecting me to write some bytes and then truncate them.

Just putting this out there for the next confused person since Google
made no mention of this!
 

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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top