Perl quoting convention that avoids excessive backslashes

K

Kelly Jones

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?
 
S

Stefano Crocco

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top