为什么Rubymine建议我在字符串上使用单引号而不是双引号?

时间:2022-09-15 16:18:31

Every time I use double-quoted strings, I'm getting this kind of suggestion:

每次我使用双引号字符串,我都会得到这样的建议:

为什么Rubymine建议我在字符串上使用单引号而不是双引号?

When I click the bulb icon I'm getting an option to convert that string into a single-quoted string.

当我点击灯泡图标时,我可以选择将该字符串转换为单引号字符串。

Can someone explain why single-quoted strings are preferred over double-quoted strings?

有人可以解释为什么单引号字符串比双引号字符串更受欢迎吗?

3 个解决方案

#1


21  

Single quotes are preferred if there is no interpolation in the string. Ruby will work less (in theory) to output single quote strings which in turn will speed up your code some (again in theory). That is one reason why RubyMine suggests it.

如果字符串中没有插值,则首选单引号。 Ruby(理论上)将减少输出单引号字符串的工作量,这反过来会加速你的代码(理论上再次)。这就是RubyMine建议的原因之一。

Another reason is for plain readability. Which you can read about here in the style guide: Ruby Coding Style Guide

另一个原因是易读性。您可以在样式指南中阅读:Ruby编码样式指南

Benchmark tests has proven that speed gains from using single over double quoted strings is negligible compared to the actual execution time.

基准测试已经证明,与实际执行时间相比,使用单个超过双引号字符串的速度增益可以忽略不计。

Ultimately the answer comes down to style. To learn about performance check out this question: Is there a performance gain in using single quotes vs double quotes in ruby?

最终答案归结为风格。要了解性能,请查看以下问题:在ruby中使用单引号与双引号是否有性能提升?

#2


5  

There used to be a performance difference, so rubymine(and most of the community) recommended single quotes when you were not interpolating.

曾经存在性能差异,因此当您没有插值时,rubymine(以及大多数社区)推荐使用单引号。

The perf difference no longer exists. http://viget.com/extend/just-use-double-quoted-ruby-strings

性能差异不再存在。 http://viget.com/extend/just-use-double-quoted-ruby-strings

You can disable the warnings:

您可以禁用警告:

Preferences > Editor > Inspections > Ruby > Double quoted string

首选项>编辑器>检查> Ruby>双引号字符串

Unfortunately you cannot reverse the warnings and show warnings for single-quoted strings.

遗憾的是,您无法撤消警告并显示单引号字符串的警告。

#3


1  

Single quotes will preserve escape characters such as as \n whereas these characters will escape double quotes.

单引号将保留转义字符,例如\ n,而这些字符将转义双引号。

I don't currently have RubyMine installed as my trial ran out, but I'm willing to bet you can change this in preferences if you would prefer to use double quotes and the suggestions bother you.

我目前没有安装RubyMine,因为我的试用版用完了,但我愿意打赌你可以在首选项中改变它,如果你更喜欢使用双引号并且建议打扰你。

#1


21  

Single quotes are preferred if there is no interpolation in the string. Ruby will work less (in theory) to output single quote strings which in turn will speed up your code some (again in theory). That is one reason why RubyMine suggests it.

如果字符串中没有插值,则首选单引号。 Ruby(理论上)将减少输出单引号字符串的工作量,这反过来会加速你的代码(理论上再次)。这就是RubyMine建议的原因之一。

Another reason is for plain readability. Which you can read about here in the style guide: Ruby Coding Style Guide

另一个原因是易读性。您可以在样式指南中阅读:Ruby编码样式指南

Benchmark tests has proven that speed gains from using single over double quoted strings is negligible compared to the actual execution time.

基准测试已经证明,与实际执行时间相比,使用单个超过双引号字符串的速度增益可以忽略不计。

Ultimately the answer comes down to style. To learn about performance check out this question: Is there a performance gain in using single quotes vs double quotes in ruby?

最终答案归结为风格。要了解性能,请查看以下问题:在ruby中使用单引号与双引号是否有性能提升?

#2


5  

There used to be a performance difference, so rubymine(and most of the community) recommended single quotes when you were not interpolating.

曾经存在性能差异,因此当您没有插值时,rubymine(以及大多数社区)推荐使用单引号。

The perf difference no longer exists. http://viget.com/extend/just-use-double-quoted-ruby-strings

性能差异不再存在。 http://viget.com/extend/just-use-double-quoted-ruby-strings

You can disable the warnings:

您可以禁用警告:

Preferences > Editor > Inspections > Ruby > Double quoted string

首选项>编辑器>检查> Ruby>双引号字符串

Unfortunately you cannot reverse the warnings and show warnings for single-quoted strings.

遗憾的是,您无法撤消警告并显示单引号字符串的警告。

#3


1  

Single quotes will preserve escape characters such as as \n whereas these characters will escape double quotes.

单引号将保留转义字符,例如\ n,而这些字符将转义双引号。

I don't currently have RubyMine installed as my trial ran out, but I'm willing to bet you can change this in preferences if you would prefer to use double quotes and the suggestions bother you.

我目前没有安装RubyMine,因为我的试用版用完了,但我愿意打赌你可以在首选项中改变它,如果你更喜欢使用双引号并且建议打扰你。