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-what';单引号和双引号的区别是什么?_Ruby - Fatal编程技术网

Ruby-what';单引号和双引号的区别是什么?

Ruby-what';单引号和双引号的区别是什么?,ruby,Ruby,因此,我将遵循以下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(l

因此,我将遵循以下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

是的,单引号字符串不处理ASCII转义码,也不进行字符串插值

name = 'Joe'
greeting = 'Hello, #{name}' # this won't produce "Hello, Joe" 

真正懒惰的人不会键入代码,他们会复制/粘贴。:)我很惊讶,如果一个教程让你打开文件并对其进行写入,那么在解释字符串如何工作的过程中就不会花时间。我也厌倦了如此频繁地键入
“\n”
,所以我使用了
$/
@Charles。@steenslag Ah。好!!这让我感觉好多了