File.write bug

W

Wouter Smeenk

------=_Part_4884_12844533.1170852498560
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hello all,

I'm relatively new to ruby and I think I've found a bug. It doesn't
seem to be in my code.

I written this test script:

# test.rb #####################
File.open("blaat.txt","w") do |f|
f.write "test\r\n"
f.write "test\r"
f.write "test\n"
end
############################

And it produces unexpected results. It seems to insert a "\r" before
every "\n". I have included the result of this script on my computer.

I'm running Windows XP SP2 and ruby 1.8.5.

Does anyone know what the problem could be and how to fix it?

Wouter Smeenk

------=_Part_4884_12844533.1170852498560
Content-Type: text/plain; name=blaat.txt; charset=ANSI_X3.4-1968
Content-Transfer-Encoding: base64
X-Attachment-Id: f_exvr8rvu
Content-Disposition: attachment; filename="blaat.txt"

dGVzdA0NCnRlc3QNdGVzdA0K
------=_Part_4884_12844533.1170852498560--
 
C

Chris Shea

Hello all,

I'm relatively new to ruby and I think I've found a bug. It doesn't
seem to be in my code.

I written this test script:

# test.rb #####################
File.open("blaat.txt","w") do |f|
f.write "test\r\n"
f.write "test\r"
f.write "test\n"
end
############################

And it produces unexpected results. It seems to insert a "\r" before
every "\n". I have included the result of this script on my computer.

I'm running Windows XP SP2 and ruby 1.8.5.

Does anyone know what the problem could be and how to fix it?

Wouter Smeenk

[blaat.txt]test
test test

It's not a bug.

On Windows, "\n" evaluates to a carriage return and a line feed, what
you may be used to calling "\r\n". If you open blaat.txt in SciTE (or
any similar text editor) and view the ends of lines (View -> End of
Line), you'll see.

So what's happening is that f.write "test\r\n" is writing a carriage
return, then "\n" which is a carriage return and a line feed.
 
J

Jan Svitok

Hello all,

I'm relatively new to ruby and I think I've found a bug. It doesn't
seem to be in my code.

I written this test script:

# test.rb #####################
File.open("blaat.txt","w") do |f|
f.write "test\r\n"
f.write "test\r"
f.write "test\n"
end
############################

And it produces unexpected results. It seems to insert a "\r" before
every "\n". I have included the result of this script on my computer.

I'm running Windows XP SP2 and ruby 1.8.5.

Does anyone know what the problem could be and how to fix it?

Wouter Smeenk

[blaat.txt]test
test test

It's not a bug.

On Windows, "\n" evaluates to a carriage return and a line feed, what
you may be used to calling "\r\n". If you open blaat.txt in SciTE (or
any similar text editor) and view the ends of lines (View -> End of
Line), you'll see.

So what's happening is that f.write "test\r\n" is writing a carriage
return, then "\n" which is a carriage return and a line feed.

...and if you don't like the behaviour, use File.open('xxxx', 'wb') -
notice the 'b'.
Or specify mode as IO::WRONLY | IO::BINARY

The same works for Python as well.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top