String escape mystery

I can’t figure out what’s happening here:

shadowfax:~ rick$ ruby -ve’p “a\b\c”’
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.2]
-e:1: unterminated string meets end of file

shadowfax:~ rick$ ruby -e’p “a\b\c”’
“a\b\c”

shadowfax:~ rick$ ruby -e’puts “a\b\c”’
\c
shadowfax:~ rick$

What’s up with \c in a string literal?


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Rick Denatale wrote:

What’s up with \c in a string literal?

“\cx” and “\C-c” mean both “Control-c”.

Wolfgang Nádasi-Donner

Wolfgang Nádasi-Donner wrote:

Rick Denatale wrote:

What’s up with \c in a string literal?

“\cx” and “\C-c” mean both “Control-c”.

Wolfgang Nádasi-Donner

Sorry, typo -> “\cc” and “\C-c” mean both “Control-c”.

Rick Denatale wrote:

I can’t figure out what’s happening here:

shadowfax:~ rick$ ruby -ve’p “a\b\c”’
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.2]
-e:1: unterminated string meets end of file

shadowfax:~ rick$ ruby -e’p “a\b\c”’
“a\b\c”

shadowfax:~ rick$ ruby -e’puts “a\b\c”’
\c
shadowfax:~ rick$

What’s up with \c in a string literal?


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Hu?
I fail to see what exactly is your problem. \b is backspace, so the a
becomes deleted in the output. In the inspect of course it still shows
up. \ is a literal \ and that is followed by a c, so “\c” naturally
prints as \c
Did I miss your point or so?

Regards
Stefan

Rick Denatale wrote:

On Nov 17, 2007 5:52 PM, Stefan R. [email protected] wrote:

http://talklikeaduck.denhaven2.com/

Hu?
I fail to see what exactly is your problem. \b is backspace, so the a
becomes deleted in the output. In the inspect of course it still shows
up. \ is a literal \ and that is followed by a c, so “\c” naturally
prints as \c
Did I miss your point or so?

No it was just a brain fart on my part.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Please take a look at…

irb(main):001:0> “a\b\c”"
=> “a\b\002”

…and the documentation in
Programming Ruby: The Pragmatic Programmer's Guide in
“Basic Types”, subchapter “Strings” is a table…

Substitutions in double-quoted strings

\a Bell/alert (0x07) \nnn Octal nnn
\b Backspace (0x08) \xnn Hex nn
\e Escape (0x1b) \cx Control-x
\f Formfeed (0x0c) \C-x Control-x
\n Newline (0x0a) \M-x Meta-x
\r Return (0x0d) \M-\C-x Meta-control-x
\s Space (0x20) \x x
\t Tab (0x09) #{expr} Value of expr
\v Vertical tab (0x0b)

…which explains everything in detail.

Wolfgang Nádasi-Donner

On Nov 17, 2007 5:52 PM, Stefan R. [email protected] wrote:

http://talklikeaduck.denhaven2.com/

Hu?
I fail to see what exactly is your problem. \b is backspace, so the a
becomes deleted in the output. In the inspect of course it still shows
up. \ is a literal \ and that is followed by a c, so “\c” naturally
prints as \c
Did I miss your point or so?

No it was just a brain fart on my part.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/