Created
March 5, 2015 09:31
-
-
Save buithehoa/f9a6431ab7f6be38ee07 to your computer and use it in GitHub Desktop.
Sanitize a Ruby string to be used as a file name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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