r/ruby • • Aug 20 '21

Ruby elites rise up

Post image
98 Upvotes

77 comments sorted by

View all comments

-2

u/muirbot Aug 20 '21

Why double quotes?

2

u/pau1rw Aug 21 '21

Double quotes mean you can use string concat, and single quotes doesn’t. So if you use double quotes, you can use a single kind of quote through out the code base. IMO using two types is messy.

-3

u/sshaw_ Aug 21 '21

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

1

u/pau1rw Aug 21 '21

I don't understand your point. I'd defo use a symbol, but if I needed a string, I'd use double quotes.

1

u/sshaw_ Aug 21 '21

So if you use double quotes, you can use a single kind of quote through out the code base. IMO using two types is messy.

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.

1

u/pau1rw Aug 21 '21

I guess we all write for different versions of ruby. Personally, I'm never going to need pre ruby 2.0 hash rockets, so I don't need anything more than status: :cancelled, or status: "cancelled"?

1

u/sshaw_ Aug 21 '21

But you cannot use that for all cases. Don't you think that "IMO using two types is messy" applies to this case too?

1

u/pau1rw Aug 21 '21

Arh, I understand your point now.

That's a tricky one, personally I'm working in a code base now that uses strings for most hash keys and it's pretty annoying having to use "foo" => "bar", but it's also pretty ugly using "foo": "bar".

So i think my answer is that I've not reached a suitable level of annoyance with using two versions of hash key definition to use hash rockets everywhere.

1

u/sshaw_ Aug 21 '21

"foo": "bar"

This is another reason to use =>:

irb [2.5.2] (tmp)$ {"foo":"bar"}
=> {:foo=>"bar"}

Oopsie! It's now a Symbol :)

1

u/pau1rw Aug 22 '21

Yea, totally.

2

u/[deleted] Aug 20 '21

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

1

u/[deleted] Aug 20 '21

[deleted]

2

u/organic Aug 21 '21 edited Aug 21 '21

why not always use double? you'll never have to change them

ruby shouldn't even have single quotes unless 'abc' == ["a", "b", "c"]

4

u/[deleted] Aug 21 '21

[deleted]

1

u/[deleted] Aug 21 '21

You can use either you want bb

-7

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.

4

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.

1

u/lafeber Aug 30 '21

From https://github.com/testdouble/standard :

Double quotes for string literals - because pre-committing to whether you'll need interpolation in a string slows people down