关于encoding utf-8_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > 关于encoding utf-8

关于encoding utf-8

 2014/3/27 23:05:28  夜鸣猪  程序员俱乐部  我要评论(0)
  • 摘要:一gem解决方案https://github.com/m-ryan/magic_encoding二,raketask解决方案desc"ManagetheencodingheaderofRubyfiles"task:check_encoding_headers=>:environmentdofiles=Array.new["*.rb","*.rake"].eachdo|extension|files.concat(Dir[File.join(Dir.getwd.split(/\\/)
  • 标签:
一 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




-
上一篇: 汽车 4G 时代拉开序幕,众厂商争相上马 下一篇: 没有下一篇了!
  • 相关文章
发表评论
用户名: 匿名