欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于encoding utf-8

程序员文章站 2022-05-25 19:22:32
...
一 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\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




-
相关标签: ruby