Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save buithehoa/f9a6431ab7f6be38ee07 to your computer and use it in GitHub Desktop.
Save buithehoa/f9a6431ab7f6be38ee07 to your computer and use it in GitHub Desktop.
Sanitize a Ruby string to be used as a file name
def friendly_filename(filename)
filename.gsub(/[^\w\s_-]+/, '')
.gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
.gsub(/\s+/, '_')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment