Ruby 1 9以後字串編碼方式的變化

2021-08-31 12:54:01 字數 3387 閱讀 6324

在某次為專案的乙個應用程式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'

/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/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

Ruby1 9之字串內編碼和外編碼

puts encoding.default external puts encoding.default internal win7下預設輸出 encoding.default external gbk encoding.default internal ruby1.9的字串編碼改動非常大。在rub...

ruby 1,9 多位元組字元

ruby 1.9 string類 length 與 size 方法返回字元數 bytesize 方法返回位元組數 encoding方法返回字串的編碼方式 force encoding方法顯示地設定乙個字串的編碼方式,注 它不改變底層的位元組 text stram.readline.force enc...

ruby分割字串 Ruby字串

構建方法 str hello world 只允許 與 轉義 str hello world 允許所有轉義和 字串拼接 str q hello world 等同單引號 str q 等同雙引號 str hello world eosstr abc 2 abcabc 索引str abc s str 1 s...