Zabba
Zabba

Reputation: 65467

How to make Ruby ignore backslash in strings?

Is there some way in Ruby that I can avoid having to put double-backslash in Ruby strings (like what can be done in C#):

For example, in C# was can prefix a string with @ and then the backslash in the string does not need to be escaped:

@"C:\Windows, C:\ABC"

Without @ we would need to escape the backslash:

"C:\\Windows, C:\\ABC"

Is there something similar in Ruby?

Upvotes: 3

Views: 2583

Answers (2)

LuisVM
LuisVM

Reputation: 2793

You can also use %q and backslashes will be automatically escaped for you:

%q{C:\Windows} => "C:\\Windows"

Upvotes: 1

Kyle Heironimus
Kyle Heironimus

Reputation: 8041

Use single quotes

my_string = 'C:\Windows'

See more in the Strings section here

Upvotes: 7

Related Questions