ruby 转化ANSI 编为UTF-8

用到:gem: rchardet19,string.encoding,string.force_encoding



require 'rchardet19'


def convert_encode file
string=File.open(file.location,"r+").read #
cd = CharDet.detect(string)
# 获取文件编码
#cd.encoding
if string.encoding != 'UTF-8' && cd.encoding=="utf-8"
string.force_encoding('UTF-8')
elsif string.encoding != 'UTF-8'
string.force_encoding('GBK')
end
File.open(file.location, "w") { |f| f.puts string.encode("UTF-8") }
end

你可能感兴趣的:(ruby 转化ANSI 编为UTF-8)