Calamity Lane

Curious Code for Curious Coders

Multi-line Strings in Ruby-- I Love Carriage Returns!

If you’ve ever done any serious development in VB or the old VBScript, you’ve probably done something like this:

1
2
3
4
5
strSQL = "SELECT o.* " & _
         "FROM people p " & _
         "INNER JOIN orders o " & _
         "ON o.person_id = p.id " & _
         "WHERE person_id = " & PersonID & ";"

…which is highly readable, but a huge pain to debug, especially when trying to cut-n-paste into the query window. How many of us have learned BkSp-BkSp-BkSp-Down-End-repeat-ad-infinitum? In fact, if you were to take every ‘” & _’ and ’ “’ I’ve ever deleted and stack them up on top of one another, you would probably end up with a stack… well, several inches high. Let’s face it, electrons just aren’t that big.

Fed up with this state of affairs, many of us moved to C# for development, leading to:

1
2
3
4
5
strSQL =          "SELECT o.* ";
strSQL = strSQL + "FROM people p ";
strSQL = strSQL + "INNER JOIN orders o ";
strSQL = strSQL + "  ON o.person_id = p.id ";
strSQL = strSQL + "WHERE person_id = " + PersonID.ToString() + ";

In the computer industry, this is called “progress.”

Of course, if you wanted to make debugging simpler, you could stuff the entire string on one line:

1
strSQL = "SELECT o.* FROM people p INNER JOIN orders o ON o.person_id = p.id WHERE person_id = " + PersonID.ToString() + ";"

…which is a lot easier to cut-n-paste, but a lot less easy to read.

Which is where Ruby comes in. Same query in Ruby:

1
2
3
4
5
6
strSQL = "
      SELECT o.*
        FROM people p
        INNER JOIN orders o ON o.person_id = p.id
        WHERE person_id = #{person_id.to_s};
    "

Isn’t it bee-yoo-tee-ful? Readable, functional, and oh so copy-pasteable. Also, it does not require extra punctuation to patch together a simple multi-line query.

There are about 19,000 different ways to define a string in Ruby, but it’s the simple timesavers like this that make the language really shine.

Of course, if you were using Rails, you could just

1
Person.find(person_id).orders

…which just goes to show how much smarter and more sexually attractive Railers are than everybody else.

NOTE: Originally, I was going to include connection and execution of the query, but that would have blown the VBScript and Ruby examples up an extra four lines of code each. Using recommended practices, it would have bloated the C# code up another 15 lines, plus a stored procedure definition.

The Rails example would still have been only one line. Simplicity ftw!