2013-03-25 26 views
0

我想出來的unidecoder寶石和它給我的問題有一些字符串:問題捕unidecoder例外

require 'unidecoder' 
str = "\u00A3" 
str.to_ascii 

#:(C:/Ruby193/lib/ruby/gems/1.9.1 /gems/unidecoder-1.1.1/lib/unidecoder/data/x00.yml): 從C:/Ruby193/lib/ruby/1.9解析報價d標量行 2列3 時發現未知轉義字符。 1/psych.rb:203:在parse' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in parse_stream' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:151:在parse' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:127:in從C:/ Ruby193/lib/r加載' uby/1.9.1/psych.rb:297:在block in load_file' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:in中打開' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:load_file' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:8:in block' from C:/ Ruby193/lib /ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in yield' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in 默認' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder -1.1.1/lib/unidecoder.rb:78:在 decode_char' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:39:in 塊中解碼' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb :37:在 gsub' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:37:in 解碼 ' 從C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:16:在 to_ascii' from (irb):21 from C:/Ruby193/bin/irb:12:in' >>

更糟的是,我不能這樣做捕獲錯誤:

foo = str.to_ascii rescue 'x' 

有誰知道這裏發生了什麼?

+0

什麼是您當前的系統'LOCALE'?關於救援:[救援條款沒有參數列表,參數默認爲StandardError](http://rubylearning.com/satishtalim/ruby_exceptions.html);它看起來像「unidecoder」引發了其他異常,但堆棧跟蹤似乎是不完整的(它應該顯示異常類型)。 – mudasobwa 2013-03-25 04:42:47

+0

@mudasobwa - 我認爲這一定是它。我的印象是,沒有參數的救援抓住了一切。如果你想把它放在答案中,我會接受。 – pguardiario 2013-03-25 05:05:31

回答

1

看看「C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder/data/x00.yml」。第2行是YAML條目- "\z",它不是Ruby中的有效轉義序列(但是標記字符串結尾的Regexp錨點)。這可能是一個錯誤。您可以將此行編輯爲- "\x00"

但是,"\u00A3"(£)不是有效的ASCII字符,我沒有找到將它編碼爲ASCII的點。

引發的異常是Psych :: SyntaxError,您可以捕獲該特定異常,如@mudasobwa評論。

+0

我會編輯它,但我想我已經看到儘可能多的unicoder,因爲我想在這一點上。 – pguardiario 2013-03-25 05:09:10