学习Ruby艰难之路第9章三重引号

时间:2022-03-30 22:27:12

Zed Shaw's Learn Ruby the Hard Way chapter 9 uses triple double quotes:

Zed Shaw's Learn Ruby the Hard Way第9章使用三重双引号:

puts """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""

I tried writing the same thing with single double quotes and it seems to work fine. I don't understand the difference between triple and single double quotes. Am I missing something?

我尝试用单双引号写同样的东西,它似乎工作正常。我不明白三重和单双引号之间的区别。我错过了什么吗?

2 个解决方案

#1


10  

I don't know why he uses triple double quotes in his book. They're nothing special, and one double quote works just fine.

我不知道他为什么在他的书中使用三重双引号。它们没什么特别的,一个双引号工作得很好。

This is a little known "feature" of ruby - it simply glues adjacent strings together.

这是红宝石的一个鲜为人知的“特征” - 它只是将相邻的弦粘在一起。

s = "hello " "world" # equivalent to "hello " + "world"
s # => "hello world"

So your example is equivalent to

所以你的例子相当于

puts "" + "
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
" + ""

More string tricks: http://pivotallabs.com/stupid-ruby-quoting-tricks/

更多字符串技巧:http://pivotallabs.com/stupid-ruby-quoting-tricks/

#2


5  

I think you found a mistake in the book - the reason he uses triple quotes may be that Python allows you to write strings over multiple lines only when using triple quotes, so it might have been that he took over the example from "Learning Python the Hard Way" or simply mixed up languages.

我认为你在本书中发现了一个错误 - 他使用三引号的原因可能是Python只允许你在使用三引号时在多行上编写字符串,所以可能是他从“学习Python”中接过了这个例子。艰难的方式“或简单混淆语言。

In Ruby, you can include new lines even in single quotes.

在Ruby中,您甚至可以在单引号中包含新行。

Let him know about that, I'm sure he'll appreciate it.

让他知道,我相信他会很感激。

#1


10  

I don't know why he uses triple double quotes in his book. They're nothing special, and one double quote works just fine.

我不知道他为什么在他的书中使用三重双引号。它们没什么特别的,一个双引号工作得很好。

This is a little known "feature" of ruby - it simply glues adjacent strings together.

这是红宝石的一个鲜为人知的“特征” - 它只是将相邻的弦粘在一起。

s = "hello " "world" # equivalent to "hello " + "world"
s # => "hello world"

So your example is equivalent to

所以你的例子相当于

puts "" + "
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
" + ""

More string tricks: http://pivotallabs.com/stupid-ruby-quoting-tricks/

更多字符串技巧:http://pivotallabs.com/stupid-ruby-quoting-tricks/

#2


5  

I think you found a mistake in the book - the reason he uses triple quotes may be that Python allows you to write strings over multiple lines only when using triple quotes, so it might have been that he took over the example from "Learning Python the Hard Way" or simply mixed up languages.

我认为你在本书中发现了一个错误 - 他使用三引号的原因可能是Python只允许你在使用三引号时在多行上编写字符串,所以可能是他从“学习Python”中接过了这个例子。艰难的方式“或简单混淆语言。

In Ruby, you can include new lines even in single quotes.

在Ruby中,您甚至可以在单引号中包含新行。

Let him know about that, I'm sure he'll appreciate it.

让他知道,我相信他会很感激。