I am trying to get the picture about how “\n” works in Ruby.
Since this is somewhat difficult to concrete in a single question I have
broken the issue in a few ones. Let’s see how it works.
Does “\n” play the role of logical newline? In a broad sense, if that
was the case portable line-oriented code would use “\n” as newline for
reading and writing through non-binmoded streams. (True in Perl, false
in Java.)
Is “\n”.length == 1 no matter the platfrom? (True in Perl, true in
Java.)
Does “\n” == “\x0a” hold no matter the platform? (False in Perl, true
in Java.)
Does “\n” get translated by the underlying I/O system back and forth
to native newlines in non-binmoded streams? (True in Perl, false in
Java.)
Thank you very much, and regards from warm Barcelona,
I am trying to get the picture about how “\n” works in Ruby.
Since this is somewhat difficult to concrete in a single question I have
broken the issue in a few ones. Let’s see how it works.
Does “\n” play the role of logical newline? In a broad sense, if that
was the case portable line-oriented code would use “\n” as newline for
reading and writing through non-binmoded streams. (True in Perl, false
in Java.)
Yes. But you can change that either by setting $/ to something else or by
using IO#each or IO#each_line with a string used as record (i.e. line)
separator.
Is “\n”.length == 1 no matter the platfrom? (True in Perl, true in
Java.)
Yes.
Does “\n” == “\x0a” hold no matter the platform? (False in Perl, true
in Java.)
I think so.
Does “\n” get translated by the underlying I/O system back and forth
to native newlines in non-binmoded streams? (True in Perl, false in
Java.)
I think so. On win platform i always receive lines with a single “\n” at
the end, although the OS uses “\r\n”.
Thank you very much, and regards from warm Barcelona,
Warm regards from warm Paderborn to warm Barcelona
I am trying to get the picture about how “\n” works in Ruby.
Okay.
Does “\n” play the role of logical newline?
Does “\n” get translated by the underlying I/O system back and forth
to native newlines in non-binmoded streams?
Yes. For example, in text-mode streams under Windows,
outputting “\n” from Ruby results in “\r\n” being written,
and “\r\n” becomes “\n” when read into Ruby. Under pre-X Mac OS,
outputting “\n” from Ruby results in “\r” being written, and
“\r” becomes “\n” when read into Ruby.
Is “\n”.length == 1 no matter the platform?
Does “\n” == “\x0a” hold no matter the platform?
Yes. Within the Ruby code, “\n” always represents a string of
length 1 whose single character (which, at least through 1.8,
means single byte) has the decimal value 10. The conversion to
other strings happens only on input and output.
(False in Perl)
Yes, but only because of MacPerl, in which “\n” represents chr(13)
everywhere while “\r” represents chr(10) everywhere. This is one of
several MacPerl design decisions that in retrospect may not have been
the way to go, but with which we now seem to be stuck.