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

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

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

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

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