String escape mystery.

R

Rick DeNatale

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?
 
S

Stefan Rusterholz

Rick said:
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
 
R

Rick DeNatale

Rick said:
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?


No it was just a brain fart on my part.
 
W

Wolfgang Nádasi-Donner

Rick said:
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
http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html 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
 

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