Skip to content

Instantly share code, notes, and snippets.

@jcf
Created January 6, 2012 10:05
Show Gist options
  • Save jcf/1569952 to your computer and use it in GitHub Desktop.
Save jcf/1569952 to your computer and use it in GitHub Desktop.
Add encoding to Ruby files
#!/usr/bin/env ruby
@file_count = 0
@skip_count = 0
@line_count = 0
def unshift_encoding(path)
lines = File.open(path).readlines
if lines.first =~ /encoding: utf-8/
@skip_count += 1
puts "Skipping #{path}" if $VERBOSE
else
@line_count += lines.length
@file_count += 1
lines.unshift("# encoding: utf-8\n")
File.open(path, 'w') {|f| f.write lines.join }
end
end
paths = Dir['./vendor/plugins/**/*.rb']
# paths = Dir['./app/models/**/*.rb']
# paths << Dir['./app/controllers/**/*.rb']
# paths << Dir['./lib/**/*.rb']
paths.flatten.each do |path|
unshift_encoding(path)
end
puts "Summary:"
puts " #{@file_count.to_s.rjust(4)} files"
puts " #{@line_count.to_s.rjust(4)} lines"
puts " #{@skip_count.to_s.rjust(4)} skipped"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment