用Ruby查找替换UTF-8文件中的中文字符

[b]Precondition:[/b]
1. The Source Java file is saved as UTF-8 type
2. The Source java file containts some chinese words as comments for some test data.
3. My ruby code file is saved as UTF-8 as well.

[b]Requirement:[/b]
1. Find all the '克' in the java files under a foder, and repleace it to '可'
2. User runs the replace.rb file through command line like:
replace <dir name>

[b]Code: replace.rb[/b]

##############################################################
dName = ARGV[0] # Directory to process
srcStr = '克' # srcStr = ARGV[1].dup.force_encoding('utf-8')
destStr = '可' # destStr = ARGV[2].dup.force_encoding('utf-8')
##############################################################
Dir.chdir(dName)
Dir["**/*.java"].each do |file|

oldFileName = File.basename(file,".java") + ".org"
File.rename(file, oldFileName)

oldFile = File.open(oldFileName, "r")
newFile = File.open(file, "w")

puts "........................... #{File.basename(newFile)}"
oldFile.each_line do |line|
line.force_encoding('utf-8')
puts oldFile.lineno, line if line =~ /#{srcStr}/
newFile.puts line.gsub(/#{srcStr}/, destStr)
end

oldFile.close
newFile.close

end


[b]Issue:[/b]
I'm new to Ruby. The coding style and error handling is not enough

I failed to pass the chinese words as command arguments by above code like : replace <dir name> <sourceString>, <replaceString>
Example: replace <dir name> "克" "可"
This way doesn't work. I don't know the cause so far.

[b]Udpaded on 26/01/2011[/b]
I found the solution:
change the source code from
srcStr = ARGV[1].dup.force_encoding('utf-8')
destStr = ARGV[2].dup.force_encoding('utf-8')

to
srcStr = ARGV[1].dup.encode('utf-8')
destStr = ARGV[2].dup.encode('utf-8')


This can make the paramemter passed and convert to UTF-8 successfully.

Good article:
[url]http://blog.grayproductions.net/articles/ruby_19s_string[/url]

But later I tried to change the code
line.force_encoding('utf-8')

to
line.encode('utf-8')


It has error again. :(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值