Skip to content

Instantly share code, notes, and snippets.

@dosire
Created July 9, 2010 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dosire/469423 to your computer and use it in GitHub Desktop.
Save dosire/469423 to your computer and use it in GitHub Desktop.
def to_filename(name)
result = name.downcase
result.gsub!(/\W/, ' ') # Remove any non word chars
result.gsub!(/\ /, '_') # Convert spaces to underscores
result.gsub!(/(_)$/, '') # Trim underscores at end
result.gsub!(/^(_)/, '') # Trim underscores at beginning
result.gsub!(/( )$/, '') # Trim spaces at end
result.gsub!(/^( )/, '') # Trim spaces at beginning
result = result[0..59]
(result.blank?) ? Time.now.to_i : result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment