Skip to content

Instantly share code, notes, and snippets.

@entropie
Last active April 11, 2025 13:05
Show Gist options
  • Save entropie/ea4a09b26cf53155571433cc0026f9d2 to your computer and use it in GitHub Desktop.
Save entropie/ea4a09b26cf53155571433cc0026f9d2 to your computer and use it in GitHub Desktop.
require 'find'
def remove_trailing_whitespaces(directory)
Find.find(directory) do |path|
next unless File.file?(path) # Skip directories
next if path =~ /\.\/\.git/
begin
text = File.open(path, "r:UTF-8") do |file|
file.read.encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
end
modified_text = text.gsub(/[ \t]+$/, '')
puts path
File.open(path, "w:UTF-8") { |file| file.write(modified_text) } unless text == modified_text
rescue => e
puts "Error processing file #{path}: #{e.message}"
end
end
end
# Call the function with the path to your repository
remove_trailing_whitespaces('.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment