我一直在学习Ruby Koans,但是对于“转义字符和单引号字符串”的例子感到困惑。
其中一个例子展示了我不能以这种方式使用转义字符,但紧接着下面的例子却给出了:
def test_single_quotes_sometimes_interpret_escape_characters
string = '\\\''
assert_equal 2, string.size # <-- my answer is correct according to the program
assert_equal "\\'", string # <-- my answer is correct according to the program
end
这让我困惑了两个方面:
- 单引号有时可以与转义字符一起使用。
- 当
assert_equal
是"\\\'"
时,为什么字符串大小为2
? (我个人认为答案应该是"\'"
,这在大小方面更有意义)。