3

It may be a bit trivial question, but always bothered me:

Are there any specific reasons for Rails to use double quoted strings instead of single quoted strings (as rubocop suggests) in schema.rb?

ogirginc
  • 4,948
  • 3
  • 31
  • 45
  • Because single quoted strings are not actually faster. The single quoted strings cop is mostly a piece of opinionated BS. https://stackoverflow.com/questions/1836467/is-there-a-performance-gain-in-using-single-quotes-vs-double-quotes-in-ruby – max Aug 17 '17 at 17:35
  • Another answer in the same post says because it follows convention. https://stackoverflow.com/questions/1836467/is-there-a-performance-gain-in-using-single-quotes-vs-double-quotes-in-ruby#answer-1836838 – Lwin Htoo Ko Aug 17 '17 at 17:40
  • Have you ever looked at `puts 'pancakes'.inspect`? `String#inspect` uses double quotes `#inspect` is a super easy way to deal with escaping in a string that is getting dumped into Ruby source code. – mu is too short Aug 17 '17 at 17:44
  • 1
    To be honest, I was not thinking about performance benefits, but the semantic benefits. When I see a double quote, I kind of expect a string interpolation or something interesting, etc. Maybe, it's just me! :) @max Rubocop creator Bozdihar Batsov seems to agree too: https://www.viget.com/articles/just-use-double-quoted-ruby-strings#comment-1810997001 – ogirginc Aug 17 '17 at 17:47
  • I have never seen a schema.rb that used double quoted strings to escape something/anything. I guess, I need to read more code. :) @muistooshort – ogirginc Aug 17 '17 at 17:49
  • 1
    That's because most people will be working with reasonable names in their database but AR can't really assume that all the table, column, constraint, ... names are reasonable. I'm with you on using double quoted strings as a signal that escaping or interpolation is coming but a lot of Ruby people use double quotes because that's probably what they learned. – mu is too short Aug 17 '17 at 18:10

2 Answers2

1

You may find it useful to exclude the schema file from Rubocop's inspections by adding it to the .rubocop.yml file:

AllCops:
  Exclude:
    - db/schema.rb
Dr.Seuss
  • 1,598
  • 3
  • 16
  • 20
0

The reason schema.rb is using double quotes is that most of the formatting is done by calling #inspect on a number of predefined strings, e.g.:

# schema_dumper.rb:91
stream.puts "  enable_extension #{extension.inspect}"

And String#inspect prints the string with surrounding double quotes, i.e.:

"foo".inspect
#=> "\"foo\""

That said, RuboCop is there to catch human mistakes, so there's no need to include files generated by the framework, which are not meant to be edited manually. (You would still want to inspect application files created using the Rails generators.)

Drenmi
  • 8,492
  • 4
  • 42
  • 51