Speed up development with full-stack environments for every branch.

Learn More

Sanitize string [Ruby, Ruby Class, gsub and non-destructive]

83 Runs 1349 Views 240 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.

Saved

Saved

arledgemike 8

arledgemike
published 4 years ago

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.

Your session has timed out.

Dismiss (the page may not function properly).