Created
October 29, 2008 22:05
-
-
Save zeke/20844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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