Forum: Ruby Trailing \ in %qnot treated as literal???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9a830cb42e972717b75f67f604304ed1?d=identicon&s=25 NoOne (Guest)
on 2005-11-20 01:47
(Received via mailing list)
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?
D9179cdd918879d0510dfc56411e4772?d=identicon&s=25 discordantus (Guest)
on 2005-11-20 02:52
(Received via mailing list)
On 11/19/05, Richard Lionheart <NoOne@nowhere.net> 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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 kevin.olbrich (Guest)
on 2005-11-20 03:04
(Received via mailing list)
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
9a830cb42e972717b75f67f604304ed1?d=identicon&s=25 NoOne (Guest)
on 2005-11-20 11:31
(Received via mailing list)
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.
This topic is locked and can not be replied to.