Skip to content

Instantly share code, notes, and snippets.

@grosser
Forked from zeke/gist:20844
Created August 24, 2011 19:30
Show Gist options
  • Save grosser/1168961 to your computer and use it in GitHub Desktop.
Save grosser/1168961 to your computer and use it in GitHub Desktop.
# drop this in a ruby file in my_rails_app/config/initializers
# restart your rails and app you're good to go!
class String
# remove middle from strings exceeding max length.
def ellipsize(options={})
max = options[:max] || 40
delimiter = options[:delimiter] || "..."
return self if self.size <= max
remainder = max - delimiter.size
offset = remainder / 2
(self[0,offset + (remainder.odd? ? 1 : 0)].to_s + delimiter + self[-offset,offset].to_s)[0,max].to_s
end unless defined? ellipsize
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment