Prev: Help with leap year programing
Next: ruby gem installation problem on MAC PC which is behind firewall
From: Bill Kelly on 25 Jun 2010 15:53 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 25 Jun 2010 16:06 > 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 25 Jun 2010 19:44 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 25 Jun 2010 20:35 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
First
|
Prev
|
Pages: 1 2 Prev: Help with leap year programing Next: ruby gem installation problem on MAC PC which is behind firewall |