Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 单引号字符串的不一致转义行为_Ruby_String_Escaping - Fatal编程技术网

Ruby 单引号字符串的不一致转义行为

Ruby 单引号字符串的不一致转义行为,ruby,string,escaping,Ruby,String,Escaping,在以下示例中,单引号字符串的转义规则看起来不一致: Ruby转义单引号字符串的确切规则是什么 p str1 = 'a\b\c' #=> "a\\b\\c" looks fine, I know single quotes don't do escaping p str2 = 'a\\b\\c' #=> "a\\b\\c" hmm? It actually escapes # Trying double quotes p str3 = "a\b\c" #=> Error, \

在以下示例中,单引号字符串的转义规则看起来不一致: Ruby转义单引号字符串的确切规则是什么

p str1 = 'a\b\c'
#=> "a\\b\\c" looks fine, I know single quotes don't do escaping
p str2 = 'a\\b\\c'
#=> "a\\b\\c" hmm? It actually escapes

# Trying double quotes
p str3 = "a\b\c" 
#=> Error, \c isn't valid
p str4 = "a\\b\\c"
#=> "a\\b\\c" 

p str1 == str4, str2 == str4
# true, true

单引号字符串仅支持两个转义序列:
\'
–单引号
\\
–单反斜杠
除了这两个转义序列外,单引号之间的所有内容都按字面处理

资料来源: