Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rubymine suggests I use single-quotes over double-quotes on strings?

Tags:

ruby

rubymine

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

enter image description here

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?

like image 354
PrivateUser Avatar asked Mar 18 '14 03:03

PrivateUser


People also ask

What is the difference between single and double quotes to delineate strings?

The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value.

Do strings require single or double quotes?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!

Why do we use double quotes for strings?

Double quotes eliminate the need to escape apostrophes (as in contractions). Consider the string: "I'm going to the mall" , vs. the otherwise escaped version: 'I\'m going to the mall' . Double quotes mean a string in many other languages.

What is the main difference between single quotes and double quotes Ruby?

The basic difference between these two methods is Single quotes can not hold/print escape sequences directly, while double quotes can. i.e. Double quoted strings are used for String Interpolation in Ruby.


1 Answers

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.

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

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?

like image 128
MrPizzaFace Avatar answered Sep 20 '22 11:09

MrPizzaFace