Forum: Ruby Perl quoting convention that avoids excessive backslashes

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.
28d29a4ce7a31ca51aa31adbbb87c51a?d=identicon&s=25 Kelly Jones (Guest)
on 2008-11-22 22:20
(Received via mailing list)
Perl lets you quote strings like this:

$foo = qq%This string has both "quotes" and 'apostrophes'%;

to avoid excessive backslashing.

Does ruby have anything similar?
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2008-11-22 22:27
(Received via mailing list)
Alle Saturday 22 November 2008, Kelly Jones ha scritto:
> Perl lets you quote strings like this:
>
> $foo = qq%This string has both "quotes" and 'apostrophes'%;
>
> to avoid excessive backslashing.
>
> Does ruby have anything similar?

Yes:

%Q["abc 'def]

and

%["abc 'def]

create a string which can contain string interpolation:

%Q[{1+1}]
=> "2"

%q["abc 'def]

creates a string without string interpolation:

%q[#{1+1}]
=> "#{1+1}"

You can replace the [] with (), {} or <> or with any non alphanumeric
character (that is any character excluding letters and digits):

%q{"abc"}
%q!abc!

I hope this helps

Stefano
This topic is locked and can not be replied to.