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-3 of 3 total  RSS 

Create strings for SEO-friendly URLs

this method returns string, which is perfect for SEO-friendly URLs.

features:
- converts every improper character to a hyphen
- returns a lowercase string

I POSTED AN EVEN BETTER METHOD ON MY BLOG: http://tinyurl.com/2vurbq

   1  
   2  def create_callname (title)
   3  	title.downcase.gsub(/[^a-z0-9]+/i, '-')
   4  end

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

automate linking to Twitter users

this method searches for @some_user in a string (txt) and links found matches to the twitter-page of some_user

   1  
   2  def link_twitter_user(txt)
   3  	if match = txt.match(/.*?(@)((?:[a-z][a-z]+))(:|\s)/i)
   4  		user = match[2]
   5  		txt.gsub!(user, '<a href="http://twitter.com/' + user + '">' + user + '</a>')
   6  	end
   7  	txt
   8  end
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS