Which style of Ruby string quoting do you favour?

后端 未结 9 1806
挽巷
挽巷 2020-11-30 00:31

Which style of Ruby string quoting do you favour? Up until now I\'ve always used \'single quotes\' unless the string contains certain escape sequences or interp

9条回答
  •  鱼传尺愫
    2020-11-30 01:10

    I used to use single quotes until I knew I needed interpolation. Then I found that I was wasting a lot of time when I'd go back and have to change some single-quotes to double-quotes. Performance testing showed no measurable speed impact of using double-quotes, so I advocate always using double-quotes.

    The only exception is when using sub/gsub with back-references in the replacement string. Then you should use single quotes, since it's simpler.

    mystring.gsub( /(fo+)bar/, '\1baz' )
    mystring.gsub( /(fo+)bar/, "\\1baz" )

提交回复
热议问题