[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ENTER character

greg

8/13/2007 9:04:00 AM

Hi All,

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

Thanks,
Greg

8 Answers

Alex Young

8/13/2007 9:06:00 AM

0

greg wrote:
> Hi All,
>
> How can you encode a ENTER? In case of space: \s, what about ENTER?
If you mean a newline, it's "\n".

--
Alex

Ronald Fischer

8/13/2007 10:23:00 AM

0

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

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

--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162

Phrogz

8/13/2007 3:09:00 PM

0

On Aug 13, 3:03 am, greg <greg.johnse...@gmail.com> wrote:
> 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?

dda

8/13/2007 3:36:00 PM

0

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

--
dda

On 8/13/07, Phrogz <phrogz@mac.com> wrote:
> On Aug 13, 3:03 am, greg <greg.johnse...@gmail.com> wrote:
> > 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?
>
>
>

John Joyce

8/13/2007 3:58:00 PM

0

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.




Xavier Noria

8/13/2007 4:16:00 PM

0

On Aug 13, 2007, at 5:36 PM, dda wrote:

> 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


Xavier Noria

8/13/2007 4:29:00 PM

0

2007/8/13, Xavier Noria <fxn@hashref.com>:

> 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

Wolfgang Nádasi-donner

8/13/2007 6:17:00 PM

0

Gavin Kistner wrote:
> 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
--
Posted via http://www.ruby-....