# Sanitize string for shell commands - http://stackoverflow.com/q/33905127/5290909

String.class_eval do
    def sanitizeshell()
        # Escape every character except letters and shell special chars
        self.gsub!(/[^\s"-*,-<>-~\u00FF]/, '\\\\\0')
    end
    def escapenonascii()
        # Escape every character outside the ASCII range
        self.gsub!(/[[:^ascii:]]/, '\\\\\0')
    end
end



# Test it
str = "(dir *.txt & dir *Sè\u00E1ñ*.rb) | sort /R >Filé.txt 2>&1"
puts 'String:'
puts str

puts "\nSanitized:"
puts str.sanitizeshell