双引号与单引号

发布于 11-15 22:53 字数 167 浏览 6 评论 0原文

我是否应该在特定时间使用 ""''

我大部分时间都使用单引号,因为这样更容易输入,但我不确定是否应该这样做。

例如 get 'user/new'get "user/new"

Is there a specific time when I should use "" vs ''?

I've been using single quotes most of the time because it's easier to type but I'm not sure if I should.

e.g. get 'user/new' vs. get "user/new"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

相权↑美人2024-11-22 22:53:05

" " 允许您进行字符串插值,例如:

world_type = 'Mars'
"Hello #{world_type}"

" " allows you to do string interpolation, e.g.:

world_type = 'Mars'
"Hello #{world_type}"
半葬歌2024-11-22 22:53:05

除了插值之外,另一个区别是“转义序列”在单引号中不起作用

puts 'a\nb' # just print a\nb 
puts "a\nb" # print a, then b at newline 

except the interpolation, another difference is that 'escape sequence' does not work in single quote

puts 'a\nb' # just print a\nb 
puts "a\nb" # print a, then b at newline 
青衫儰鉨ミ守葔2024-11-22 22:53:05

Ruby 中的单引号 '' 和双引号 "" 之间在计算字符串的内容方面存在差异。

首先,我想澄清一下,在字符串的文字形式中,引号之间的任何内容都会被评估为字符串对象,这是 Ruby String 类的一个实例。

因此,'stackoverflow'“stackoverflow” 都将计算 String 类的实例没有任何区别

区别

字符串的两种文字形式(单引号或双引号)之间的本质区别是双引号允许转义序列,而单引号则不允许!

创建的字符串文字单引号不支持字符串插值转义序列

一个简洁的例子是:

"\n" # will be interpreted as a new line

'\n' # will display the actual escape sequence to the user

用单引号进行插值根本不起作用:

'#{Time.now}'
=> "\#{Time.now}" # which is not what you want..

最佳实践

大多数 Ruby Linters 建议对字符串使用单引号文字,并且在字符串中使用双引号插值/转义序列的情况。

There is a difference between single '' and double quotes "" in Ruby in terms of what gets to be evaluated to a string.

Initially, I would like to clarify that in the literal form of a string whatever is between single or double quotes gets evaluated as a string object, which is an instance of the Ruby String class.

Therefore, 'stackoverflow' and "stackoverflow" both will evaluate instances of String class with no difference at all.

The Difference

The essential difference between the two literal forms of strings (single or double quotes) is that double quotes allow for escape sequences while single quotes do not!

A string literal created by single quotes does not support string interpolation or escape sequences.

A neat example is:

"\n" # will be interpreted as a new line

whereas

'\n' # will display the actual escape sequence to the user

Interpolating with single quotes does not work at all:

'#{Time.now}'
=> "\#{Time.now}" # which is not what you want..

Best Practice

As most of the Ruby Linters suggest use single quote literals for your strings and go for the double ones in the case of interpolation/escaping sequences.

§对你不离不弃2024-11-22 22:53:05

要回答您的问题,当您想要进行字符串插值时,必须使用 ""

a = 2
puts "#{a}"

否则请使用简单的引号。

另外,如果您想知道性能方面是否存在差异,有一个很好的 StackOverflow 上有关于这个的问题。

如果你真的是 RoR 新手,我强烈建议你拿起一本像样的 Ruby 书来学习基础知识语言。它将帮助您理解您在做什么(并且不会让您认为 Rails 很神奇)。我个人推荐The Well grounded Rubyist

To answer your question, you have to use "" when you want to do string interpolation:

a = 2
puts "#{a}"

Use simple quotes otherwise.

Also if you are wondering about whether there is a difference in terms of performance, there is an excellent question about this on StackOverflow.

And if you are really new to RoR, I urge you to pick up a decent Ruby book to learn the basics of the language. It will help you understand what you are doing (and will keep you from thinking that Rails is magic). I personally recommend The Well grounded Rubyist.

最舍不得你2024-11-22 22:53:05

与打印中的答案“\n”类似,以下是差异的另一种情况

puts "\1"  -> get special character
puts '\1'  -> get \1

,因此看起来 * 被转换为双引号中的转义字符,但不是单引号中的转义字符。
顺便说一句,在正则表达式中使用时它会影响输出
例如,
str.gsub(/正则表达式/, '\1,\2')

Similar to the answer "\n" in printing, following is another case of the difference

puts "\1"  -> get special character
puts '\1'  -> get \1

so looks like * was convert to escaped character in double quotes, but not in single quotes.
BTW, it will impact the output when using in regular expression
e.g.,
str.gsub(/regular expression/, '\1,\2')

猫性小仙女2024-11-22 22:53:05

您想要使用单引号的另一个原因是当将正则表达式模式作为字符串传递时:

此正则表达式模式将起作用,因为在单引号内传递:

"123 ABC".match('\d')
=> #<MatchData "1">

此正则表达式模式将失败,因为在双引号内传递(您必须双引号)逃避它以使其正常工作):

"123 ABC".match("\d")
=> nil

Another reason you would want to use single-quotes is when passing a regex pattern as a string:

This regex pattern will work because passed within single-quotes:

"123 ABC".match('\d')
=> #<MatchData "1">

This regex pattern will fail because passed within double-quotes (you would have to double-escape it to get it to work):

"123 ABC".match("\d")
=> nil
情未る2024-11-22 22:53:05

在这种特定情况下,如何编写它没有什么区别。它们是等价的。另外,您可能还想阅读更多 Ruby 指南/教程 :)

In this specific case, it makes no difference how you write it. They are equivalent. Also, you may want to read some more Ruby guides/tutorials :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文