Logo: Relish

  1. Sign in

Project: Ruby-style-guide

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)
    
  • Consider padding string interpolation code with space. It more clearly sets the
    code apart from the string.

    "#{ user.last_name }, #{ user.first_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 second style is arguably a bit more popular in the Ruby
    community. The string literals in this guide, however, 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, use String#<<. Concatenation mutates the string instance in-place
    and is always faster than String#+, which creates a bunch of new string objects.

    # good and also fast
    html = ''
    html << '<h1>Page title</h1>'
    
    paragraphs.each do |paragraph|
      html << "<p>#{paragraph}</p>"
    end
    
  • 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"
    

Last published over 6 years ago by David Kariuki.