Is there any advantage to _ever_ using a single quote around a string in Ruby / Rails?

I understand the functional difference between single and double quotes in Ruby, but I wonder what specific reasons people have for different between them. In my opinion, it seems like you should always use a double quote and not think about it.

A couple of reasoning that I read in a study of the topic ...

  • Use a single quote if you do not need a double quote.

  • There is a very, very slight performance advantage for single quotes.

Any other interesting thoughts? (Or maybe this is a case of freedom or Ruby, leaving the door open so that no one can do something ...)

+6
source share
6 answers

I usually follow the following rule:

never use double quotes (or %Q or %W ) unless you interpolate

The reason for this is that if you are trying to track down a security or security error, you immediately know when you look at the beginning of a line that there can be no code inside it, so there can be no error.

However, I also follow the following exception to the rule:

use double quotes if they make the code more readable

those. I prefer

 "It time" 

above

 'It\ time' %q{It time} 

Technically, single quotes are infinitely faster to process than double quotes, but that doesn’t matter because

  • the program is processed only once, during startup there is no difference in execution performance
  • the difference in performance is very small.
  • the time spent parsing the strings doesn't matter compared to the time spent syntaxing the Ruby crazier syntax.

So, the answer to your question: Yes, there is an advantage, namely that you will immediately notice if the line can contain code.

+10
source

I can imagine three reasons for using single quotes:

  • They look cleaner (the reason I use them)
  • They make it easy to create a string that you would have to avoid ( 'he said "yes"' vs "he said \"yes\"" )
  • They are a little more effective.
+4
source

I would suggest that using a single quote string is faster, because double quotes allow interpolation of strings, but single quote strings do not.

This is the only difference I know of. For this reason, it is probably best to use only one quotation mark if you do not need string interpolation:

 num = 59 "I ate #{num} pineapples"` 
+1
source

Well, there is a lot of confusion regarding the "performance boost" of single quotes and double quotes.

The fact is that it doesn't really matter if you don't interpolate. There are many guidelines on the Internet that support this claim. ( fooobar.com/questions/50667 / ... )

Personally, I use double for strings that have interpolation just for readability. I prefer to see double quotes when I need them. But there are actually methods in ruby ​​for interpolating strings other than double quoting:

 %q{#{this} doesn't get interpolated} %Q{#{this} is interpolated} 
+1
source
 1.9.2-p290 :004 > x = 3 => 3 1.9.2-p290 :005 > "#{x}" => "3" 1.9.2-p290 :006 > '#{x}' => "\#{x}" 

In any other case, I prefer single quotes because it is easier to type and just makes the code less obscured before my eyes.

0
source

Asking this question, I discovered this unofficial Ruby style guide that addresses this, and many other style questions that I had floating around in my head. I highly recommend checking this out.

0
source

Source: https://habr.com/ru/post/912143/


All Articles