encoding utf-8について

2210 ワード

1 gemソリューション
https://github.com/m-ryan/magic_encoding
二、rake taskソリューション

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

" ["

", "
"].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

"+content) new_file.close end end

三、日常的な解決策

#encoding: utf-8

#config/application.rb
config.encoding = "utf-8"

違いがあり、1つのコンテンツ、1つのソースコード
試した、無駄な解決策

#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


-