开发者

How to make Ruby ignore backslash in strings?

开发者 https://www.devze.com 2023-02-03 08:26 出处:网络
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#):

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 es开发者_JAVA百科caped:

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

Without @ we would need to escape the backslash:

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

Is there something similar in Ruby?


Use single quotes

my_string = 'C:\Windows'

See more in the Strings section here


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

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

精彩评论

暂无评论...
验证码 换一张
取 消