Truncate text with word boundaries in Ruby
1 2 def truncate_words(text, length = 30, end_string = ' …') 3 words = text.split() 4 words[0..(length-1)].join(' ') + (words.length > length ? end_string : '') 5 end
DZone Snippets > chao > string
12731 users tagging and storing useful source code snippets
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
Chao Lam http://blogs.coolchaser.com
1 2 def truncate_words(text, length = 30, end_string = ' …') 3 words = text.split() 4 words[0..(length-1)].join(' ') + (words.length > length ? end_string : '') 5 end