简体   繁体   English

双引号与单引号

[英]Double vs single quotes

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.我大部分时间都使用单引号,因为它更容易打字,但我不确定是否应该使用。

eg get 'user/new' vs. get "user/new"例如get 'user/new'get "user/new"

" " allows you to do string interpolation, eg: " "允许您进行字符串插值,例如:

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

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 

There is a difference between single '' and double quotes "" in Ruby in terms of what gets to be evaluated to a string.在 Ruby 中,单''和双引号""在评估字符串的内容方面存在差异。

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.最初,我想澄清一下,在字符串的字面形式中引号或引号之间的任何内容都被评估为字符串对象,它是 Ruby String 类的一个实例。

Therefore, 'stackoverflow' and "stackoverflow" both will evaluate instances of String class with no difference at all .因此, 'stackoverflow'"stackoverflow"都将评估 String 类的实例,完全没有区别

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 interpollation and does not 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.由于大多数 Ruby Linter 建议对您的字符串使用单引号文字,在插值/转义序列的情况下使用双引号

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.此外,如果您想知道在性能方面是否存在差异, 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.如果你真的是 RoR 的新手,我强烈建议你拿起一本像样的 Ruby 书来学习这门语言的基础知识。 It will help you understand what you are doing (and will keep you from thinking that Rails is magic).它会帮助你理解你在做什么(并且不会让你认为 Rails 是魔法)。 I personally recommend The Well grounded Rubyist .我个人推荐The Well grounded Rubyist

A single-quoted strings don't process ASCII escape codes( \\n, \\t etc), and they don't do string interpolation while double-quoted does both.单引号字符串不处理 ASCII 转义码(\\n、\\t 等),并且它们不执行字符串插值,而双引号则两者都执行。

Escape code example:转义码示例:

2.4.0 :004 >   puts 'Hello \n World'
Hello \n World

2.4.0 :005 > puts "Hello \n World"
Hello
World

Interpolation example:插值示例:

2.4.0 :008 >   age=27
 => 27

2.4.0 :009 > puts 'Age: #{age}'
Age: #{age}

2.4.0 :009 > puts "Age: #{age}"
Age: 27

Similar to the answer "\\n" in printing, following is another case of the difference与打印中的答案“\\n”类似,以下是另一种不同的情况

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 eg, str.gsub(/regular expression/, '\\1,\\2')顺便说一句,在正则表达式中使用时会影响输出,例如 str.gsub(/regular expression/, '\\1,\\2')

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

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 :)此外,您可能想阅读更多 Ruby 指南/教程 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM