Long Ruby Strings :: The Here-Document
Posted by Chris Blackburn
I am fairly anal about my code and the code that my firm (CBCI) produces. I like it to be formatted perfectly. It makes me cringe to even have a single extra space out of place, borking the indentation. Using tabs for indentation instead of spaces should be grounds for termination. :-D
In addition to perfect indentation, I don’t like scrolling to the right in my editor to view long lines of code. This can usually be handled properly by adding linefeeds. Consider the following example:
sql = %Q{
SELECT
users.id,
users.username,
users.email,
users.created_at,
users.activated_at,
users.confirmed_at,
affiliations.custom1 as affiliation_custom1,
affiliations.custom2 as affiliation_custom2,
affiliations.custom3 as affiliation_custom3,
campaigns.name as affiliation_campaign_name
FROM users
LEFT OUTER JOIN affiliations ON affiliations.user_id = users.id
LEFT OUTER JOIN campaigns ON campaigns.id = affiliations.campaign_id
}There are several ways to handle this type of long string. First, for SQL statements, extra spaces in front of each line don’t matter, nor do the linefeeds. So the code above, using the ‘%Q’ method, would work just fine for building a string with spaces embedded. We could also use the lowercase ‘%q’ method which would wrap the string with single-quotes and prevent any escape sequences or interpolation of embedded expressions, which is fine since we don’t have any need for that in the above string. You could also wrap the quotation marks, single or double, around the string explicitly.
If you are working with a long character string, and are fussy about your code being formatted consistently, the indents will embed groups of spaces into your strings. As stated above, this technically doesn’t matter when considering SQL statements. In other cases where it does matter we use the Ruby HERE document as follows:
# first we extend the String class
class String
def here_with_pipe
lines = self.split("\n")
lines.map! {|c| c.sub!(/\s*\|/, '')}
new_string = lines.join(" ")
self.replace(new_string)
end
endThis strips off the leading whitespace and pipe character that gets embedded in the string using the HERE document like this:
s = <<-end.here_with_pipe
|This string will be concatenated
|into a single line
|containing no extra embedded spaces
|and no line feeds.
endIsn’t Ruby cool!

Thanks for the Q%{} shorthand, its useful. However I am curious as to why you’d need to write entire SQL query as a string if you are using ActiveRecord?
As for the here_with_pipe method, it can be a bit more efficient by using the regexp: