# String escape mystery

**URL:** <https://www.ruby-forum.com/t/string-escape-mystery/120517>\
**Category:** Ruby\
**Created:** [November 17, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/string-escape-mystery/120517 "2007-11-17T21:10:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [November 17, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/string-escape-mystery/120517/1 "2007-11-17T21:10:12Z")

</div>

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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [November 17, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/string-escape-mystery/120517/2 "2007-11-17T21:22:13Z")

</div>

Rick Denatale wrote:

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

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

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [November 17, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/string-escape-mystery/120517/3 "2007-11-17T21:22:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [November 17, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/string-escape-mystery/120517/4 "2007-11-17T23:52:05Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [November 18, 2007, 12:14am UTC](https://www.ruby-forum.com/t/string-escape-mystery/120517/5 "2007-11-18T00:14:31Z")

</div>

Rick Denatale wrote:

> On Nov 17, 2007 5:52 PM, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > [http://talklikeaduck.denhaven2.com/](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/](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](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

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [November 17, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/string-escape-mystery/120517/6 "2007-11-17T23:59:18Z")

</div>

On Nov 17, 2007 5:52 PM, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > [http://talklikeaduck.denhaven2.com/](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/](http://talklikeaduck.denhaven2.com/)
