From: Xavier Noria Date: 2012-10-31T16:23:05+09:00 Subject: Re: newline conversion on reading? --e89a8ff1cc28012f7304cd55c4da Content-Type: text/plain; charset=UTF-8 On Wed, Oct 31, 2012 at 3:08 AM, 7stud -- wrote: 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. --e89a8ff1cc28012f7304cd55c4da Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, Oct 31, 2012 at 3:08 AM, 7stud -- <lists@ruby-forum.com>= wrote:

Xavier Noria wrote in post #1082114:
> I can't get newline conversions to work while reading:
>
> =C2=A0 =C2=A0 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". =C2=A0So lets look through your output and= convert any
"\n" characters to "\r\n" to see what is actually writt= en 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"

>
> =C2=A0 =C2=A0 File.open('foo.txt', 'r', universal_newl= ine: true) {|fh| fh.read}
> =C2=A0 =C2=A0 # =3D> "foo\r\nbar\r\n", no normalization p= erformed
>
> 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 fi= le
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


T= hat test ran on a Mac.

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

--e89a8ff1cc28012f7304cd55c4da--