views:

274

answers:

7

Is there support in Ruby for (for lack of a better word) non-escaped (verbatim) strings?

Like in C#:

@"c:\Program Files\"

...or in Tcl:

{c:\Program Files\}
+4  A: 

Yes, you need to prefix your string with % and then a single character delineating its type.

The one you want is %q{c:\program files\}.

The pickaxe book covers this nicely here, section is General Delimited Input.

DevelopingChris
why not just use single quotes?
banister
typically because you need quotes as apostrophes or multiple quotes, or unbalanced quotes in your string.
DevelopingChris
Just updating the link the the pickaxe book as .com seems to be gone now.http://www.rubycentral.org/pickaxe/language.html
Mike Stead
thanks, updated in the link in the post to that url.
DevelopingChris
@banister: Single quotes work, but only if you're willing to escape all those backslashes with more backslashes, which can get ugly in cases such as Windows file paths. For obvious reasons, leaving it as `'c:\program files\'` will not work as intended.
Matchu
+1  A: 

This has a couple examples that may be what you are looking for: Ruby Syntax

Sean Bright
+2  A: 

Besides %q{string}, you can also do the following:

string =<<SQL
  SELECT * 
  FROM Book
  WHERE price > 100.00
  ORDER BY title;
SQL

The delimiters are arbitrary strings, conventionally in uppercase.

Antonio Cangiano
A: 
mystring = %q["'\t blahblahblah]

Or if you want to interpret \t as tab:

mystring = %Q["'\t blahblahblah]
Zsolt Botykai
+2  A: 

You can just use a single quoted string.

>> puts "a\tb"
a    b
=> nil
>> puts 'a\tb'
a\tb
=> nil
Farrel
A: 

I can't seem to find anything that doesn't escape... I've tried all of these, maybe I've got an old version of ruby. Basically, no matter how I try to write a\b, it always escapes the central slashes to a single one.

A: 

Even this board escaped it - I mean a\\b

You can edit your comments, no need to post a second answer.
Joachim Sauer