ENTER character

G

greg

Hi All,

How can you encode a ENTER? In case of space: \s, what about ENTER?

Thanks,
Greg
 
R

Ronald Fischer

How can you encode a ENTER? In case of space: \s, what about ENTER?

If you mean "carriage return", it is '\r'

--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
 
P

Phrogz

How can you encode a ENTER? In case of space: \s, what about ENTER?

On MacOS (before version X), pressing the 'return' key inserted a
carriage return (CR) character, ASCII value 13, represented in a Ruby
string by "\r".

On *nix-variants (including Mac OS X), pressing the (enter/return) key
inserts a line feed (LF) character, ASCII value 10, represented in a
Ruby string by "\n".

On Windows, pressing the 'enter' key key inserts a CR/LF pair,
representable in a Ruby string as "\r\n".

Thankfully, MacOS is no more, and now there are only two flavors to
work with. Unfortunately, more disparities abound than just text
files.

In Firefox (and Safari) on Windows, the line endings for <textarea>
elements in web pages are exposed to JS as LF characters. In IE on
Windows, the line endings are CR/LF pairs. (I don't have any other
browsers/OS readily available to test right now.)

Does that help?
 
D

dda

Actually, depending on what text editor you are using, on Mac OS X,
ASCII(13) may still be used.
 
J

John Joyce

To make a long story shorter, it depends, and you do need to check
for any of these potential situations if they are likely to occur in
your program. If they're unlikely, don't worry about it.
The recommendations you've heard are pretty well-known but there is
always the possibility of a particular application doing things its
own way... I don't know of any cases, but there must be some, if you
were dealing with especially old files.
 
X

Xavier Noria

Actually, depending on what text editor you are using, on Mac OS X,
ASCII(13) may still be used.

And no matter what, most editors let the user configure the
convention to any of those three. That's why there's a difference
between _portable_ line-oriented programs and _robust_ line-oriented
programs.

A portable line-oriented program is a one that works fine on the
assumption that the line-ending convention is the one of the runtime
platform. A log analysis tool may be written like that for example.
Programming languages provide idioms to do this very easily.

A robust line-oriented program tries to understand every convention.
For instance a CGI processing a text area. That normally requires
newline normalization by hand. Some programming languages offer I/O
options to understand everything on reading. For instance the :crlf
Perl I/O layer, or the universal line-ending mode in Python. I am not
aware of any such trick on Ruby, in that case a simple regexp may
suffice.

Writing is a different story, because the program itself is the one
using some convention. The way to choose it depends on its own logic.

-- fxn
 
X

Xavier Noria

2007/8/13 said:
A robust line-oriented program tries to understand every convention.
For instance a CGI processing a text area. That normally requires
newline normalization by hand. Some programming languages offer I/O
options to understand everything on reading. For instance the :crlf
Perl I/O layer,

Let me add that the :crlf layer is a partial solution for this , it
lets you understand text as long as it comes with CRLFs or the runtime
convention.

-- fxn
 
W

Wolfgang Nádasi-donner

Gavin said:
On Windows, pressing the 'enter' key key inserts a CR/LF pair,
representable in a Ruby string as "\r\n".

If you are reading from a file in Windows, Ruby will convert the "\r\n"
sequence to a single "\n".

Because this can cause trouble when working with binary data or when
using direct positioning one can open a file for binary processing,
simly by adding a "b" character to the type of opening the file (see
documentation for details).

When working with a file opened in binary mode, each line ends with
"\r\n".

Wolfgang Nádasi-Donner
 

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

Forum statistics

Threads
474,266
Messages
2,571,318
Members
48,002
Latest member
EttaPfeffe

Latest Threads

Top