Speed up development with full-stack environments for every branch.Learn More
Sanitize string [Ruby, Ruby Class, gsub and non-destructive]
75 Runs 1190 Views 222 Copies
A class you can use to sanitize strings to make them acceptable file names. Can also be used just for its method by making verbose FALSE otherwise its a command line tool.
class Sanitize_File def initialize(verbose = false) if verbose puts "Enter the string you want to sanitize: \n\n" @sanitize = gets.chomp @new = sanitize_filename(@sanitize) puts " \nNew file name is : [email protected]} \n\n" end end def sanitize_filename(file_name) file_name.gsub(/[^\w\.\-]/,"_") end end m = Sanitize_File.new(true)
Please login/signup to get access to the terminal.