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)

   1  
   2  def shorten (string, count = 30)
   3  	if string.length >= count 
   4  		shortened = string[0, count]
   5  		splitted = shortened.split(/\s/)
   6  		words = splitted.length
   7  		splitted[0, words-1].join(" ") + ' ...'
   8  	else 
   9  		string
  10  	end
  11  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS