Multi line strings in Ruby

Multi line strings in Ruby

Ruby has some nice ways to multi line strings. First, this one is the worst. If you are coming from Java or Javascript you might be tempted use it though.

# Do not do it this way
str = 'Lorem ipsum dolor sit amet, duo nusquam minimum id, ius suas elitr ' +
        'persius eu. Mel tamquam verterem inciderint in. Solum propriae cum ut.' +
        ' Cum utinam nonumes nominavi eu, mazim dolor per in.' +
        "\n" +
        'Debet vivendo pri ei, nec hinc labore in. Duo ad vocibus oporteat ' +
        'appellantur. Nibh idque no eos, mel viris partiendo ei, te pro ' +
        'discere diceret. Vero aliquid quo an. Porro lobortis convenire vis ' +
        'ea, copiosae epicurei percipit nam ut.'

Why is this bad?

The + is operator is a method on the string object that returns a new String object. This code will create new instances of String for each call to the + method, not the greatest thing if we are trying to preserve resource in our Ruby VM.

Instead we should use the line continuation character \

str = 'Lorem ipsum dolor sit amet, duo nusquam minimum id, ius suas elitr ' \
        'persius eu. Mel tamquam verterem inciderint in. Solum propriae cum ut.' \
        ' Cum utinam nonumes nominavi eu, mazim dolor per in.' \
        "\n" \
        'Debet vivendo pri ei, nec hinc labore in. Duo ad vocibus oporteat ' \
        'appellantur. Nibh idque no eos, mel viris partiendo ei, te pro ' \
        'discere diceret. Vero aliquid quo an. Porro lobortis convenire vis ' \
        'ea, copiosae epicurei percipit nam ut.'

This is still pretty ugly for long mutli line string. I try to only use this if I have more than a few lines.

Ruby also includes Here Documents or Heredocs. Heredocs were introduced by the Bourne shell and are found other languages like Perl. Heredocs will preserve all formatting including indentation and new lines. This format also allows variable interpolation present in Ruby’s double quoted strings.

str = <<-TEXT
  Lorem ipsum dolor sit amet, duo nusquam minimum id, ius suas elitr
  persius eu. Mel tamquam verterem inciderint in. Solum propriae cum ut.
  Cum utinam nonumes nominavi eu, mazim dolor per in.

  Debet vivendo pri ei, nec hinc labore in. Duo ad vocibus oporteat
  appellantur. Nibh idque no eos, mel viris partiendo ei, te pro
  discere diceret. Vero aliquid quo an. Porro lobortis convenire vis
  ea, copiosae epicurei percipit nam ut.
TEXT

And finally, we have the %q{ text } and %Q{ text } format. The former follows single quote rules and does not allow variable interpolation. In Ruby 2, there is also a %{ text } syntax.

str = %{
    Lorem ipsum dolor sit amet, duo nusquam minimum id, ius suas elitr
    persius eu. Mel tamquam verterem inciderint in. Solum propriae cum ut.
    Cum utinam nonumes nominavi eu, mazim dolor per in.

    Debet vivendo pri ei, nec hinc labore in. Duo ad vocibus oporteat
    appellantur. Nibh idque no eos, mel viris partiendo ei, te pro
    discere diceret. Vero aliquid quo an. Porro lobortis convenire vis
    ea, copiosae epicurei percipit nam ut.
  }

The one caveat with this format is that if you introduce a { character within the string, you must balance it out. For example, if you have a string smiley face, this will not fly.

# invalid syntax
  str = %{
    I like foo
    :}

  }