Newline conversion on reading?

I can't get newline conversions to work while reading:

    File.write('foo.txt', "foo\r\nbar\r\n")
    File.open('foo.txt', 'r', universal_newline: true) {|fh| fh.read}
    # => "foo\r\nbar\r\n", no normalization performed

Are they only supposed to work for writing? If yes, why the asymmetry?

Xavier Noria wrote in post #1082114:

I can't get newline conversions to work while reading:

    File.write('foo.txt', "foo\r\nbar\r\n")

If you are on a Window operating system, ruby will convert any "\n" in
your output to "\r\n". So lets look through your output and convert any
"\n" characters to "\r\n" to see what is actually written to the file.
This line:

foo\r..\n..bar\r..\n

gets written as:

because there are two "\n" characters that ruby converts to "\r\n"

    File.open('foo.txt', 'r', universal_newline: true) {|fh| fh.read}
    # => "foo\r\nbar\r\n", no normalization performed

Are they only supposed to work for writing? If yes, why the asymmetry?

On Windows, the conversion that ruby performs when you read from a file
is to convert any "\r\n" it sees to "\n", so this line in the file:

foo\r..\r\n..bar..\r..\r\n

gets read in as:

foo\r..\n..bar..\r..\n

···

--
Posted via http://www.ruby-forum.com/\.

Xavier Noria wrote in post #1082114:

> I can't get newline conversions to work while reading:
>
> File.write('foo.txt', "foo\r\nbar\r\n")
>

If you are on a Window operating system, ruby will convert any "\n" in
your output to "\r\n". So lets look through your output and convert any
"\n" characters to "\r\n" to see what is actually written to the file.
This line:

foo\r..\n..bar\r..\n

gets written as:

because there are two "\n" characters that ruby converts to "\r\n"

>
> File.open('foo.txt', 'r', universal_newline: true) {|fh| fh.read}
> # => "foo\r\nbar\r\n", no normalization performed
>
> Are they only supposed to work for writing? If yes, why the asymmetry?

On Windows, the conversion that ruby performs when you read from a file
is to convert any "\r\n" it sees to "\n", so this line in the file:

foo\r..\r\n..bar..\r..\r\n

gets read in as:

foo\r..\n..bar..\r..\n

That test ran on a Mac.

If you try that on a Unix machine you'll see :universal_newline is not
doing CRLF to LF conversion on reading.

···

On Wed, Oct 31, 2012 at 3:08 AM, 7stud -- <lists@ruby-forum.com> wrote:

Looking at the test suite for 1.9.3 I see you need to explicitly
enable textmode with either:

    open(filename, 'rt')
    open(filename, 'r', textmode: true)

That enables universal newline convention by default, though you can
disable it by passing universal_newline: false.