Long Ruby Strings :: The Here-Document

Posted by Chris Blackburn Wed, 23 May 2007 19:40:00 GMT

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
end

This 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.
  end

Isn’t Ruby cool!

Tags , , ,  | 1 comment | no trackbacks

Comments

  1. Avatar Konstantin Gredeskoul said 2 months later:

    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:

    def here_with_pipe
       self.replace(self.gsub(/(\n?\s+\|\s?)/m, ' ').strip)
    end
    

Trackbacks

Use the following link to trackback from your own site:
http://blog.cbciweb.com/trackbacks?article_id=long-ruby-strings-the-here-document&day=23&month=05&year=2007

(leave url/email »)

   Comment Markup Help Preview comment