C
Chris Morris
Why does Ruby translate \n -> \r\n automagically when writing to/from
file? Convenience, I assume, so I don't have to worry with this. But
I've run into some wackiness regarding this.
I'm uploading a file from a ruby script to a ruby cgi script. cgi.rb
when reading multipart assumes that \r\n will exist as terminators in
the contents. My client.rb reads in a file, Ruby translates the \r\n in
the file to \n -- I then have to retranslate this back to \r\n prior to
uploading to the cgi script, so the cgi will recognize the line
terminators properly. Then my cgi writes the contents of the uploaded
file to a local file, still translating \n -> \r\n so \r\n in the
uploaded content becomes \r\r\n when written to disk.
Argh. I don't know what I'm asking for, but I needed to rant. Is
there a better way? Or is this just a weird case because of the specific
line terminator cgi.rb multiform reader code is using?
D:\temp>more endlinetest.rb
s = "hey\r\n"
File.open('endlinetest.txt', 'w+') do |f| f.print s end
s_read = ''
File.open('endlinetest.txt', 'r') do |f| s_read = f.read end
p s_read
D:\temp>endlinetest.rb
"hey\r\n"
D:\temp>w2k_dump.exe endlinetest.txt
// w2k_dump.exe
// SBS Windows 2000 Hex Dump Utility V1.00
// 08-27-2000 Sven B. Schreiber
// (e-mail address removed)
File: endlinetest.txt
Size: 0x00000006 (6)
Address | 00 01 02 03-04 05 06 07 : 08 09 0A 0B-0C 0D 0E 0F |
0123456789ABCDEF
---------|-------------------------:-------------------------|-----------------
00000000 | 68 65 79 0D-0D 0A : - | hey...
file? Convenience, I assume, so I don't have to worry with this. But
I've run into some wackiness regarding this.
I'm uploading a file from a ruby script to a ruby cgi script. cgi.rb
when reading multipart assumes that \r\n will exist as terminators in
the contents. My client.rb reads in a file, Ruby translates the \r\n in
the file to \n -- I then have to retranslate this back to \r\n prior to
uploading to the cgi script, so the cgi will recognize the line
terminators properly. Then my cgi writes the contents of the uploaded
file to a local file, still translating \n -> \r\n so \r\n in the
uploaded content becomes \r\r\n when written to disk.
Argh. I don't know what I'm asking for, but I needed to rant. Is
there a better way? Or is this just a weird case because of the specific
line terminator cgi.rb multiform reader code is using?
D:\temp>more endlinetest.rb
s = "hey\r\n"
File.open('endlinetest.txt', 'w+') do |f| f.print s end
s_read = ''
File.open('endlinetest.txt', 'r') do |f| s_read = f.read end
p s_read
D:\temp>endlinetest.rb
"hey\r\n"
D:\temp>w2k_dump.exe endlinetest.txt
// w2k_dump.exe
// SBS Windows 2000 Hex Dump Utility V1.00
// 08-27-2000 Sven B. Schreiber
// (e-mail address removed)
File: endlinetest.txt
Size: 0x00000006 (6)
Address | 00 01 02 03-04 05 06 07 : 08 09 0A 0B-0C 0D 0E 0F |
0123456789ABCDEF
---------|-------------------------:-------------------------|-----------------
00000000 | 68 65 79 0D-0D 0A : - | hey...