From: Bill Kelly on
Roger Pack wrote:
>
> Yes, but if I read and write both in ASCII mode, should it not be
> expected to round trip? I'm a bit confused...

irb(main):001:0> [RUBY_VERSION, RUBY_PLATFORM]
=> ["1.9.2", "i386-mswin32_100"]
irb(main):002:0> File.open("zz", "w") {|io| io.write "foo\r\nbar\nbaz\n"}
=> 16
irb(main):003:0> File.read("zz")
=> "foo\n\nbar\nbaz\n"


Notice the "\r\n" came back as "\n\n".


Regards,

Bill



From: Roger Pack on

> irb(main):002:0> File.open("zz", "w") {|io| io.write
> "foo\r\nbar\nbaz\n"}
> => 16
> irb(main):003:0> File.read("zz")
> => "foo\n\nbar\nbaz\n"
>
>
> Notice the "\r\n" came back as "\n\n".

This feels like a bug to me...
--
Posted via http://www.ruby-forum.com/.

From: Bill Kelly on
Roger Pack wrote:
>> irb(main):002:0> File.open("zz", "w") {|io| io.write
>> "foo\r\nbar\nbaz\n"}
>> => 16
>> irb(main):003:0> File.read("zz")
>> => "foo\n\nbar\nbaz\n"
>>
>>
>> Notice the "\r\n" came back as "\n\n".
>
> This feels like a bug to me...

If I could pick one statement to summarize my feeling
about developing on Windows, that might be it. ;P

But anyway...

It's not strictly a ruby issue. I recall encountering CRLF
text mode vs. binary mode issues in DOS with Borland C
in the mid 1980's.

But, back to the ruby example above... I don't see
how one could expect the data to survive a round trip.

Note that if we force ruby to deal with a single
character at a time, the results are consistent with
the above:

irb(main):004:0> File.open("zz2", "w") {|io| "foo\r\nbar\nbaz\n".each_char {|c| io.write c; io.flush}}
=> "foo\r\nbar\nbaz\n"

irb(main):005:0> File.open("zz2", "r") {|io| x=c=""; x << c while (c = io.getc); x}
=> "foo\n\nbar\nbaz\n"



So... it's not clear to me how round-trip semantics
could be implemented given the need to consider each
character individually?


Regards,

Bill


From: Luis Lavena on
On Jun 25, 7:44 pm, Bill Kelly <bi...(a)cts.com> wrote:
> Roger Pack wrote:
> >> irb(main):002:0> File.open("zz", "w") {|io| io.write
> >> "foo\r\nbar\nbaz\n"}
> >> => 16
> >> irb(main):003:0> File.read("zz")
> >> => "foo\n\nbar\nbaz\n"
>
> >> Notice the "\r\n" came back as "\n\n".
>
> > This feels like a bug to me...
>
> If I could pick one statement to summarize my feeling
> about developing on Windows, that might be it.  ;P
>
> But anyway...
>
> It's not strictly a ruby issue.  I recall encountering CRLF
> text mode vs. binary mode issues in DOS with Borland C
> in the mid 1980's.
>
> But, back to the ruby example above... I don't see
> how one could expect the data to survive a round trip.
>
> Note that if we force ruby to deal with a single
> character at a time, the results are consistent with
> the above:
>
> irb(main):004:0> File.open("zz2", "w") {|io| "foo\r\nbar\nbaz\n".each_char {|c| io.write c; io.flush}}
> => "foo\r\nbar\nbaz\n"
>
> irb(main):005:0> File.open("zz2", "r") {|io| x=c=""; x << c while (c = io.getc); x}
> => "foo\n\nbar\nbaz\n"
>
> So... it's not clear to me how round-trip semantics
> could be implemented given the need to consider each
> character individually?
>

If you want to specify the new lines yourself, you need to use binary
mode.

Text mode read and write under Windows will do weird things, but is
defined as the "spec" of Ruby behavior.

--
Luis Lavena