Skip to content

Instantly share code, notes, and snippets.

@klochner
Created August 7, 2009 00:22
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 klochner/163643 to your computer and use it in GitHub Desktop.
Save klochner/163643 to your computer and use it in GitHub Desktop.
def sanitize_for_filename
#strip the string
ret = self.strip
#blow away apostrophes
ret.gsub! /['`]/,""
# @ --> at, & --> and
ret.gsub! /\s*@\s*/, " at "
ret.gsub! /\s*&\s*/, " and "
#replace all non alphanumeric, underscore or periods with underscore
ret.gsub! /\s*[^A-Za-z0-9\.\-]\s*/, '_'
#convert double underscores to single
ret.gsub! /_+/,"_"
#strip off leading/trailing underscore
ret.gsub! /\A[_\.]|[_\.]\z/,""
ret
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment