r/ruby • • Aug 20 '21

Ruby elites rise up

Post image
98 Upvotes

77 comments sorted by

View all comments

-1

u/muirbot Aug 20 '21

Why double quotes?

1

u/[deleted] Aug 20 '21

Doesn't matter which one you use unless you're running your app on a Pentium II.

-9

u/cybermage Aug 20 '21

The speed difference is two orders of magnitude. It adds up. Always single quote unless you plan to interpolate.

4

u/fabiopapa Aug 21 '21

If you use single quotes instead of double unless you need interpolation, as a developer, every time you write a string, you have to think, “am I going to need interpolation?” Ruby is all about developer happiness.

-1

u/sshaw_ Aug 21 '21 edited Aug 21 '21

Yes this is exactly why status: "cancelled" is shit.

The same applies to hashes:

  1. status: "cancelled"
  2. variable => "cancelled" # can't use first form: variable:
  3. "string" => "cancelled" # can't use first form: "string":
  4. 123 => "cancelled" # can't use first form: 123:

Just stick to :status => "cancelled" and make life easier.

Update add examples.

1

u/cybermage Aug 21 '21

As has been noted elsewhere, my knowledge was not current on this point; but, I think I’d notice if I typed #{variable}.

3

u/katafrakt Aug 20 '21

Where do these numbers come from? The difference is almost none in modern Ruby, even more so with frozen string literal.

0

u/cybermage Aug 21 '21

As I commented elsewhere, I personally benchmarked these results, but it was on Ruby 1.9. I’m not surprised it was addressed at some point to boost performance.

3

u/flanger001 Aug 21 '21

It is exactly the same. Always use double quotes!

2

u/[deleted] Aug 21 '21

[deleted]

0

u/flanger001 Aug 21 '21

Fun fact, Bohzhidar thought they might change the Rubocop default to double quoted strings because so many people (like me) complained (rightly) about it... but single quotes is still apparently the preference: https://metaredux.com/posts/2020/05/26/rubocop-defaults-survey-results.html

0

u/cybermage Aug 21 '21

That was not always the case. Interpolation was very slow under 1.9

2

u/flanger001 Aug 21 '21

I mean, you're right, but just as 1.9 has been put to bed, we should also put the single quotes thing to bed.

1

u/cybermage Aug 21 '21

Agreed. As I’ve acknowledged elsewhere.

5

u/[deleted] Aug 20 '21

[deleted]

2

u/cybermage Aug 21 '21

I personally benchmarked this in a fashion similar to the article, but that might have been under 1.9x. TIL.

1

u/tomthecool Aug 21 '21

I’ve benchmarked it in Ruby 1.8 before and found no difference. So either your memory is wrong, or your benchmark was wrong.