Single vs Double Quotes in Ruby

Single vs Double

Rubyでsingle quoteとdouble quoteを書くかは非常に迷いどころで、Rubyistたちの中でもどっちがメジャー、みたいなのはそこまでないみたい。

Based on everything I've read and a review of Ruby style guides, I would say there is no consensus. http://stackoverflow.com/questions/20870375/single-vs-double-quotes

僕はこの回答者のこの答えが好きだなと思った。

think the use of single quotes is completely understandable from the point of view that if the author has no intent to utilize the additional interpolation capabilities of the double quotes, then the use of single quotes more accurately conveys intent. Further, single quotes are easier to type and less visually "busy", so the use of them is in keeping with Ruby's character of simplicity and elegance.

確かにダブルの方が便利だけど、例えば予測しない変数展開を起こしたり(Rubyではそんなことないだろうけど笑)というところもあるかな。

あとは単純に「キレイになる」ってところと、「ここでは変数を展開したい」ということを明確化できるというところがメリットかなと思う。

かの有名なruby style guideはどっちかに統一しさえすればどっちでもいいってことになってる。ちなみにRubocopのdefaultはsingleになってる。

https://github.com/bbatsov/ruby-style-guide

GitHubはダブルクォート推し。変数展開はString#+より埋め込みを推奨してて、いつもquoteをいちいち消さずに変数を埋め込める状態に保ちたい、とダブルの方が文字列型として一般的に使われてる、という理由みたい。

https://github.com/styleguide/ruby

Cookpadのものも見たけど特に指定はなかったかな。

https://github.com/cookpad/styleguide/blob/master/ruby.ja.md#strings

結論、結構シングル派だったけど僕はGitHubの考え方も好きだなーと思った。PHPだとダブルクォートだと変数をそのまま突っ込むと展開されてしまうので、それはあんまりよろしくないなと思ってシングルを好んで使っていたけど、Rubyのような言語ならその危険性は低いから、それよりも多人数で統一することと、書きやすいコードを保持することの方が大事かなと思いました。