DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Truncate Text With Word Boundaries In Ruby
def truncate_words(text, length = 30, end_string = ' …')
words = text.split()
words[0..(length-1)].join(' ') + (words.length > length ? end_string : '')
end






Comments
Snippets Manager replied on Tue, 2008/10/07 - 3:00pm