ENTER character

Hi All,

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

Thanks,
Greg

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”.

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

If you mean “carriage return”, it is ‘\r’

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


dda

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.

On Aug 13, 3:03 am, greg [email protected] 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
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?

Gavin K. 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

On 8/13/07, Phrogz [email protected] wrote:

Ruby string by “\n”.
Well, actually terminal drivers on unix are usually responsible for the
\r
→ \n conversion, when you hit the Enter key. (When they had “Return”
written on them, it was because it was short for carriage return). You
can
turn this off with stty.

On Windows, pressing the ‘enter’ key key inserts a CR/LF pair,

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

2007/8/13, Xavier N. [email protected]:

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

Hi, the answer to your question is much more simple than written in other examples. I am currently working on a project where I needed the same information. This technically is a hack that you can manipulate into pretty much anything that you want.

If you use “gets” alone no chop or chomp and press enter that will always return “\n”

I am making a statement press enter to begin. Because I know that gets alone will result in “\n” I am using the variable => setup <= = “\n”.

After a person has pressed enter I am safeguarding against mistaken keystrokes by setting setup to = “\n”

setup = gets
setup = “\n”

Form here now that I have collected all of the data that I need to initialize an instance. The character “\n” will trigger the initialization of @*whatever.

In my case I needed some user data before starting a process.

Just to note as was mentioned before ruby will interpret ‘\n’ and “\n” to be completely different. When you use quotes that is a way of saying I am doing this intentionally. For instance if I am interpolating a variable like “Sentence that says #{variable} in here.” The same goes for including quote marks. “Sentence that says “#{variable}” in here.” Now I have said Sentence that says “variable” in here. These would not work entered as a ‘character’.

I know that sometimes it matters to be accurate. If you do not need to particularly return, enter or return. A dynamic approach will get the job done.