Elegant way of shorten a text string
def shorten (string, count = 30) if string.length >= count shortened = string[0, count] splitted = shortened.split(/\s/) words = splitted.length splitted[0, words-1].join(" ") + ' ...' else string end end
DZone Snippets > labuschin > text
12309 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
Martin Labuschin http://labuschin.com
def shorten (string, count = 30) if string.length >= count shortened = string[0, count] splitted = shortened.split(/\s/) words = splitted.length splitted[0, words-1].join(" ") + ' ...' else string end end