basic doubts on "\n"

X

Xavier Noria

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,

-- fxn
 
R

Robert Klemme

Xavier Noria said:
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

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top