11
所以我遵循这个Ruby教程:Learn Ruby the Hard Way.Ruby--单引号和双引号之间有什么区别?
在练习16(上面链接)中,您编写了一个脚本,将行写入文件。相关的代码是:
print "line 1: "; line1 = STDIN.gets.chomp()
print "line 2: "; line2 = STDIN.gets.chomp()
print "line 3: "; line3 = STDIN.gets.chomp()
puts "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
然而,作为懒惰的混混,我,我原来的例子在过去的半年线使用单引号类型,而不是双引号的教程告诉你使用。
这对文件有影响。当我用单引号,该文件是这样的:
this is line 1\nthis is line 2\nthis is line 3
切换这些引号双引号后,文件看起来如预期:
this is line 1
this is line 2
this is line 3
有人能确切地告诉我这是为什么?单引号字符串是否忽略转义字符如\n
或\t
?
是的,你是正确的 –
*真的*懒惰的人不键入代码,他们复制/粘贴。 :) –
我很惊讶,有一个教程,你打开文件并写入他们不会花时间沿途解释字符串如何工作。 –