On Dec 11, 12:34 pm, Andrei T. [email protected] wrote:
D:/Tuzhilin/ruby/ruby-1.9.1-p129-i386-mingw32/bin/irb.bat:20:in `’
Posted viahttp://www.ruby-forum.com/.
You are not using truncate correctly.
- Opening a file for “w” automatically truncates the file!
From the document from File#truncate:
truncate(file_name, integer)
Truncates the file file_name to be at most integer bytes long. Not
available on all platforms.
f = File.new(“out”, “w”)
f.write(“1234567890”) #=> 10
f.close #=> nil
File.truncate(“out”, 5) #=> 0
File.size(“out”) #=> 5
#############################################################################
I did the following experiment in irb
irb(main):032:0> f = File.new “out”, “w” => #<File:out>
irb(main):033:0> f.write(“1234567890”) =>
10
irb(main):034:0> f.close => nil
irb(main):035:0> File.truncate “out”, 5 =>
0
irb(main):036:0> File.size “out” =>
5
irb(main):037:0> File.truncate “out”, 6 =>
0
irb(main):038:0> File.size “out” =>
6
irb(main):039:0> File.truncate “out”, 0 =>
0
irb(main):040:0> File.size “out” =>
0
irb(main):041:0> File.truncate “out”, 10 =>
0
irb(main):042:0> File.size “out” => 10
So it appears that truncate adds non-printable characters if the file
originally contained fewer characters than specified.
when displayed in the “vim” editor, each added character looks like
this ‘^@’
It has been my humbling experience that when I thought I found a bug
in the code, the bug was me.
[email protected]