Skip to content

Instantly share code, notes, and snippets.

@grosser
Forked from zeke/gist:20844
Created Aug 24, 2011
Embed
What would you like to do?
# 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