# Trailing \\ in %qnot treated as literal?

**URL:** <https://www.ruby-forum.com/t/trailing-in-qnot-treated-as-literal/48413>\
**Category:** Ruby\
**Created:** [November 20, 2005, 1:47am UTC](https://www.ruby-forum.com/t/trailing-in-qnot-treated-as-literal/48413 "2005-11-20T01:47:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoOne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@NoOne](https://www.ruby-forum.com/u/NoOne)\
**Post date:** [November 20, 2005, 1:47am UTC](https://www.ruby-forum.com/t/trailing-in-qnot-treated-as-literal/48413/1 "2005-11-20T01:47:54Z")

</div>

Hi,

I ran the following on a WinXP/SP2 machine:

F:\>ruby --version  
ruby 1.8.2 (2004-12-25) [i386-mswin32]

F:\>ruby -e “puts %q[\a\b\c]”  
\a\b\c

F:\>ruby -e “puts %q[\a\b\c]”  
-e:1: unterminated string meets end of file

F:\>

It seems to me the last \ shouldn’t need to be escaped as \, though  
that  
does work. What’s up?

---

<div class="post-metadata">

**Author:** ![NoOne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@NoOne](https://www.ruby-forum.com/u/NoOne)\
**Post date:** [November 20, 2005, 2:52am UTC](https://www.ruby-forum.com/t/trailing-in-qnot-treated-as-literal/48413/2 "2005-11-20T02:52:12Z")

</div>

On 11/19/05, Richard L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> F:\>ruby -e “puts %q[\a\b\c]”  
> -e:1: unterminated string meets end of file
> 
> F:\>
> 
> It seems to me the last \ shouldn’t need to be escaped as \, though that  
> does work. What’s up?

%q[foo] is equivalent to the single-quoted string literal ‘foo’. In  
the same way, the only escapable characters are backslash and the  
character that ends the literal:

‘Mark's string’  
%q[square (]) bracket]

A literal backslash in a single quoted string only needs to be escaped  
where it might be confused as escaping the final quoting character,  
like in your example.

HTH,  
Mark

---

<div class="post-metadata">

**Author:** ![NoOne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@NoOne](https://www.ruby-forum.com/u/NoOne)\
**Post date:** [November 20, 2005, 3:04am UTC](https://www.ruby-forum.com/t/trailing-in-qnot-treated-as-literal/48413/3 "2005-11-20T03:04:15Z")

</div>

In a quoted string, a \cx is an escape for ctrl-x, so the \c] is  
interpreted as ctrl-] as the ] is an escape for the bracket in the  
quoted  
string. Since there is no close bracket you get an error.

Try this…

puts %q[\a\b\c\]

And you get

\a\b\c\

\_Kevin

---

<div class="post-metadata">

**Author:** ![NoOne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@NoOne](https://www.ruby-forum.com/u/NoOne)\
**Post date:** [November 20, 2005, 11:31am UTC](https://www.ruby-forum.com/t/trailing-in-qnot-treated-as-literal/48413/4 "2005-11-20T11:31:09Z")

</div>

Thanks. I forgot about viewing %q?..? as a generalized single-quoted  
string, as one of you said, and as Fulton said in “The Ruby Way”.  
Thanks,  
guys.
