gpt4 book ai didi

ruby - 艰难地学习 Ruby 第 9 章三重引号

转载 作者:数据小太阳 更新时间:2023-10-29 07:09:46 24 4
gpt4 key购买 nike

Zed Shaw 的 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.
"""

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

最佳答案

我不知道他为什么在他的书中使用三重双引号。它们没什么特别的,一个双引号就可以了。

这是 ruby​​ 的一个鲜为人知的“特性”——它只是将相邻的字符串粘合在一起。

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

所以你的例子等同于

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.
" + ""

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

关于ruby - 艰难地学习 Ruby 第 9 章三重引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27980199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com