Sponsors


Blog powered by TypePad

« Resolving Files with TextMate, Subversion, and FileMerge | Main | Subclipse Installation Problem on Mac OS X »

December 28, 2006

Ruby Snippet: Shortening Long Strings For Display

Ever had a long string like this:  "myveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongstring" and wanted to turn it into something much shorter, such as this: "myveryvery...longstring"? 

Here's a little snippet that does just this.  It works great in HTML select lists which, to my knowledge, are impossible to dynamically resize as the user drags their browser window larger or smaller.

Snippet is longer than necessary to be explicit.  Shorten to your heart's desire.

module StringExtensions
  # max_len is string size to shorten to if self.size is longer. 
  # Default value is 1/2 the self.size. abbrev_len is the length of
  # the abbreviation before or after "..." (depending on chop_start_end)
  # abbrev_len will be coerced up or down if < 0 or exceeds max_len - 3.
  # shorten_start_end is the part of the string to chop and replace with "..."
  # For example: "iwouldbereadbetterandfasterifiwasshortened" to a max_len
  # of 16, an abbrev_len of 1, and a chop_start_end of :start would end up
  # as "i...wasshortened"
  def three_dot_chop(max_len=size/2, abbrev_len = 3, chop_start_end=:start)
    return self if size <= max_len
    abbrev_len = 0 if abbrev_len < 0
    abbrev_len = (max_len - 3) if abbrev_len > (max_len - 3)
    chop_size = size - max_len
    case chop_start_end
    when :start
      chop_start_index = 0 + abbrev_len;
      chop_end_index = chop_size + 2 # 2 accounts for "..." as well as index access
    when :end
      chop_start_index = size - (abbrev_len + chop_size + 2)
      chop_end_index = size - abbrev_len
    end
    self[chop_start_index,chop_end_index]="..."
    self
  end
end

To use this, I suggest extending Ruby String as follows:

String.send(:include, StringExtensions)

Then, on your next long string just call it:

"myveryveryveryveryveryveryveryveryveryveryverylongstring".three_dot_chop(20)
=> "myv...verylongstring"

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/277049/7311448

Listed below are links to weblogs that reference Ruby Snippet: Shortening Long Strings For Display:

Comments

Post a comment

If you have a TypeKey or TypePad account, please Sign In