08 - Strings
Prefer string interpolation and string formatting instead of string concatenation:
# bad email_with_name = user.name + ' <' + user.email + '>' # good email_with_name = "#{user.name} <#{user.email}>" # good email_with_name = format('%s <%s>', user.name, user.email)
With interpolated expressions, there should be no padded-spacing inside the braces.
# bad "From: #{ user.first_name }, #{ user.last_name }" # good "From: #{user.first_name}, #{user.last_name}"
Adopt a consistent string literal quoting style. There are two popular styles in the Ruby community, both of which are considered good - single quotes by default (Option A) and double quotes by default (Option B).
(Option A) Prefer single-quoted strings when you don't need string interpolation or special symbols such as
\t
,\n
,'
, etc.# bad name = "Bozhidar" # good name = 'Bozhidar'
(Option B) Prefer double-quotes unless your string literal contains
"
or escape characters you want to suppress.# bad name = 'Bozhidar' # good name = "Bozhidar"
The string literals in this guide are aligned with the first style.
Don't use the character literal syntax
?x
. Since Ruby 1.9 it's basically redundant -?x
would interpreted as'x'
(a string with a single character in it).# bad char = ?c # good char = 'c'
Don't leave out
{}
around instance and global variables being interpolated into a string.class Person attr_reader :first_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end # bad - valid, but awkward def to_s "#@first_name #@last_name" end # good def to_s "#{@first_name} #{@last_name}" end end $global = 0 # bad puts "$global = #$global" # good puts "$global = #{$global}"
Don't use
Object#to_s
on interpolated objects. It's invoked on them automatically.# bad message = "This is the #{result.to_s}." # good message = "This is the #{result}."
Avoid using
String#+
when you need to construct large data chunks. Instead, useString#<<
. Concatenation mutates the string instance in-place and is always faster thanString#+
, which creates a bunch of new string objects.# bad html = '' html += '<h1>Page title</h1>' paragraphs.each do |paragraph| html += "<p>#{paragraph}</p>" end # good and also fast html = '' html << '<h1>Page title</h1>' paragraphs.each do |paragraph| html << "<p>#{paragraph}</p>" end
Don't use
String#gsub
in scenarios in which you can use a faster more specialized alternative.url = 'http://example.com' str = 'lisp-case-rules' # bad url.gsub('http://', 'https://') str.gsub('-', '_') # good url.sub('http://', 'https://') str.tr('-', '_')
When using heredocs for multi-line strings keep in mind the fact that they preserve leading whitespace. It's a good practice to employ some margin based on which to trim the excessive whitespace.
code = <<-END.gsub(/^\s+\|/, '') |def test | some_method | other_method |end END # => "def test\n some_method\n other_method\nend\n"
Use Ruby 2.3's squiggly heredocs for nicely indented multiline strings.
# bad - using Powerpack String#strip_margin code = <<-END.strip_margin('|') |def test | some_method | other_method |end END # also bad code = <<-END def test some_method other_method end END # good code = <<~END def test some_method other_method end END