Ruby 1.9以后字符串编码方式的变化

阅读更多

在某次为项目的一个应用程序memy做rake测试时出现了如下的错误:

[root@rp1 memy3.0.7]# rake --trace
error_info
rake aborted!
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/jieks-0.0.6/lib/jieks/common.rb:425: invalid multibyte char (US-ASCII)
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/jieks-0.0.6/lib/jieks/common.rb:425: invalid multibyte char (US-ASCII)
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/jieks-0.0.6/lib/jieks/common.rb:425: syntax error, unexpected $end, expecting ')'
str = str.gsub("?","-")
^
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `block in require'
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in `new_constants_in'
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `load_dependency'
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/jieks-0.0.6/lib/jieks.rb:4:in `'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:68:in `require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:66:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:66:in `block in require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:55:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:55:in `require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler.rb:120:in `require'
/var/qup/memy3.0.7/config/application.rb:7:in `'
/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/var/qup/memy3.0.7/Rakefile:4:in `'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/rake_module.rb:25:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:495:in `raw_load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:78:in `block in load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:77:in `load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:61:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/bin/rake:32:in `'
/usr/local/bin/rake:19:in `load'
/usr/local/bin/rake:19:in `
'

  

从网上调查得知,Ruby 1.9以后实现了新的字符串编码方式,而jieks-0.0.6的代码用的还是Ruby1.8的字符串机制,所以导致该错误。解决的方法是,修改出错的文件,在其开头第一行加入:
# encoding: utf-8

(或者加入 # coding: utf-8 也可\(^o^)/~)

继续执行rake命令,很多文件出现了很多同样的错误,解决办法也是一样的。
统计下来,总共有如下的文件需要作如是修正:
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/jieks-0.0.6/lib/jieks/jieks.rb
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/jieks-0.0.6/lib/jieks/qup.rb
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/jieks-0.0.6/lib/jieks/common.rb
/var/qup/memy3.0.7/thinking-sphinx/ruby/1.9.1/gems/jieksquery-0.0.11/lib/jieksquery/query.rb

 

你可能感兴趣的:(Ruby,rubygems)