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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top