字符串文字,无需转义反斜杠

Mic*_*ael 1 ruby string literals

在C#中,我可以通过@在字符串之前使用来编写反斜杠和其他特殊字符而不必转义,但我必须转义双引号.

C#

string foo = "The integer division operator in VisualBASIC is written \"a \\ b\"";
string bar = @"The integer division operator in VisualBASIC is written \"a \ b\""
Run Code Online (Sandbox Code Playgroud)

在Ruby中,我可以使用单引号字符串文字,但我想将它与字符串插值结合使用"text #{value}".Ruby中有@与C#相同的东西吗?

Har*_*pta 6

Ruby中有一些类似的东西可用.例如

foo = %Q(The integer division operator in VisualBASIC is written "a \\ b" and #{'interpolation' + ' works'})
Run Code Online (Sandbox Code Playgroud)

您还可以在其中插入字符串.唯一需要注意的是,你仍然需要逃避\角色.

HTH