Skip to content

Instantly share code, notes, and snippets.

@zeke
Created October 29, 2008 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zeke/20844 to your computer and use it in GitHub Desktop.
Save zeke/20844 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
offset = max/2
self[0,offset] + delimiter + self[-offset,offset]
end
# reduce a string to a max word count
def truncate_words(options={})
max = options[:max] || 40
ending = options[:ending] || "..."
words = text.split(" ")
return self words.size <= max
words[0..(max-1)].join(' ') + (words.size > max ? ending : '')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment