一 gem解决方案
https://github.com/m-ryan/magic_encoding
二,rake task 解决方案
class="ruby" name="code">
desc "Manage the encoding header of Ruby files"
task :check_encoding_headers => :environment do
files = Array.new
["*.rb", "*.rake"].each do |extension|
files.concat(Dir[ File.join(Dir.getwd.split(/\\/), "**", extension) ])
end
files.each do |file|
content = File.read(file)
next if content[0..16] == "# coding: UTF-8\n\n"
["\n\n", "\n"].each do |file_end|
content = content.gsub(/(# encoding: UTF-8#{file_end})|(# coding: UTF-8#{file_end})|(# -*- coding: UTF-8 -*-#{file_end})/i, "")
end
new_file = File.open(file, "w")
new_file.write("# coding: UTF-8\n\n"+content)
new_file.close
end
end
三,日常解决方案
#encoding: utf-8
#config/application.rb
config.encoding = "utf-8"
有区别,一个内容,一个源代码
试过,没用的解决方案
#config/application.rb
#我试试没管用啊
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
#solution is to use BOM http://www.w3.org/International/questions/qa-byte-order-mark or put
# encoding: UTF-8
or
# coding: UTF-8
#on top of files in utf-8.
#To set UTF-8 globally, you can put
config.encoding = "utf-8"
#in your config/application.rb which is equivalent to
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
#which in turn is the equivalent to putting:
# encoding: UTF-8
、
-