Never been to DZone Snippets before?

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

About this user

Martin Labuschin http://labuschin.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Elegant way of shorten a text string

this method shortens a plain text string down to complete words contained in given scope (count)

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
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS