Skip to content

Instantly share code, notes, and snippets.

@razenha
Created July 15, 2012 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razenha/3118663 to your computer and use it in GitHub Desktop.
Save razenha/3118663 to your computer and use it in GitHub Desktop.
A small ruby script that checks if all the .rb files in the given project starts with "# encoding: utf-8"
# encoding: utf-8
class String
def starts_with?(prefix)
prefix = prefix.to_s
self[0, prefix.length] == prefix
end
end
all_files = Dir["./**/*.rb"]
all_files.each do |file|
begin
first_line = File.open file, &:readline
if !first_line.starts_with? "# encoding: utf-8"
puts file
content = File.readlines(file)
content.insert(0, "# encoding: utf-8\n")
File.open(file, "w") { |f|
content.each { |line|
f << line
}
}
# return 0
end
rescue
puts "Erro ao ler file: #{file}"
raise $!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment