# 'ab\\c' and 'ab\\\\c'

**URL:** <https://www.ruby-forum.com/t/ab-c-and-ab-c/142558>\
**Category:** Ruby\
**Created:** [July 9, 2008, 6:58am UTC](https://www.ruby-forum.com/t/ab-c-and-ab-c/142558 "2008-07-09T06:58:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [July 9, 2008, 6:58am UTC](https://www.ruby-forum.com/t/ab-c-and-ab-c/142558/1 "2008-07-09T06:58:32Z")

</div>

## I encountered a rather peculiar behavior of strings today. Here is my irb session:

## irb(main):001:0\> ‘ab\c’ irb(main):002:0’ ’ SyntaxError: compile error (irb):2: unterminated string meets end of file from (irb):2 irb(main):003:0\> ‘ab\c’ =\> “ab\c” irb(main):004:0\> ‘a\b’ =\> “a\b” irb(main):005:0\>

As you can see when I typed ‘ab\c’ my irb didn’t return. It was  
expecting some thing more. Then I typed a single quote ( ’ ) to  
terminate the string and it gave me this SyntaxError. The same thing  
happens even I type ‘ab\c’

I was trying this after reading section 3.2.1.1 of “The Ruby  
Programming Language”. This is what it says about backslashes in  
single quoted strings:

"In single-quoted strings, a backslash is not special if the character  
that follows it is anything other than a quote or a backslash. "

But at the same time if I type ‘a\b’ it returns ‘a\b’ which is inline  
with the book. Any ideas what’s happening here?

Thanks  
subbu

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [July 9, 2008, 11:04am UTC](https://www.ruby-forum.com/t/ab-c-and-ab-c/142558/2 "2008-07-09T11:04:02Z")

</div>

I think you stumbled on another special character, \c, which seems to  
have some weird properties:

-in single quotes, must be followed by another character, but when it  
is, just swaps the \ for a \

-in double quotes, again must be followed by another character, which it  
translates into a number, though by what scheme i have no idea:

> > “ab\ca”  
> > =\> “ab\001”
> 
> > “ab\cb”  
> > =\> “ab\002”
> 
> > “ab\cA”  
> > =\> “ab\001”
> 
> > “ab\cz”  
> > =\> “ab\032”

I’m quite intrigued by this as well.

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [July 9, 2008, 11:53am UTC](https://www.ruby-forum.com/t/ab-c-and-ab-c/142558/3 "2008-07-09T11:53:33Z")

</div>

From: “Max W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I’m quite intrigued by this as well.  
> control characters.

puts “\cG”

…should beep.

puts “\cH”

…should backspace.

puts “boolean\cH\cH\cH\cHbies”

…is left as an exercise for the reader. 😉

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [July 9, 2008, 12:10pm UTC](https://www.ruby-forum.com/t/ab-c-and-ab-c/142558/4 "2008-07-09T12:10:20Z")

</div>

On Wed, Jul 9, 2008 at 11:00 AM, Max W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think you stumbled on another special character, \c, which seems to  
> have some weird properties:
> 
> -in single quotes, must be followed by another character, but when it  
> is, just swaps the \ for a \
> 
> -in double quotes, again must be followed by another character, which it  
> translates into a number, though by what scheme i have no idea:

In a double-quoted string, \c followed by another character forms a  
control character. Control characters are character codes 0 to 31 in  
the ASCII character set. When you call inspect on a string (as irb  
does for you), Ruby escapes these control characters and those are the  
number you are seeing.

In a single-quoted string, \c doesn’t do anything, just like “The Ruby  
programming language” says. Irb gets confused here and does interpret  
the \c, thinks the string goes on while it doesn’t, asks more input  
until a quote is given, which really starts a new string which is then  
unterminated and that’s the error you get.

It’s a bug in irb.

Peter

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [July 9, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/ab-c-and-ab-c/142558/5 "2008-07-09T13:42:17Z")

</div>

Ah…

There’s no index entry for \c in pickaxe (unless i’m being dumb), which  
seems like an omission.

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [July 9, 2008, 2:30pm UTC](https://www.ruby-forum.com/t/ab-c-and-ab-c/142558/6 "2008-07-09T14:30:34Z")

</div>

Interesting findings. Thanks all for the explanations.

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [July 9, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/ab-c-and-ab-c/142558/7 "2008-07-09T14:40:02Z")

</div>

On Jul 9, 2008, at 7:38 AM, Max W. wrote:

> Ah…
> 
> There’s no index entry for \c in pickaxe (unless i’m being dumb),  
> which  
> seems like an omission.

In my 2nd ed. pickaxe’s index:

String  
control characters \n etc. 321

You just have to know what to look for. 😉

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [July 9, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/ab-c-and-ab-c/142558/8 "2008-07-09T18:23:12Z")

</div>

Rob B. wrote:

> On Jul 9, 2008, at 7:38 AM, Max W. wrote:
> 
> > Ah…
> > 
> > There’s no index entry for \c in pickaxe (unless i’m being dumb),  
> > which  
> > seems like an omission.
> 
> In my 2nd ed. pickaxe’s index:
> 
> String  
> control characters \n etc. 321
> 
> You just have to know what to look for. 😉
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

You would have to know what \c means to find this out, though! That’s  
like saying the answer is in the index but the question isn’t…if you  
know what i mean.
