我正在试用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): 在行解析引号d标量时发现未知的转义字符 2栏3 来自C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:在
parse' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in
parse_stream' 来自C:/Ruby193/lib/ruby/1.9.1/psych.rb:151:inparse' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:127:in
loading' 来自C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:block in load_file' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:in
打开' 来自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
块中' 来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:inyield' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in
缺省' 来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:indecode_char' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:39:in
阻止解码' 来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:37:ingsub' 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:into_ascii' from (irb):21 from C:/Ruby193/bin/irb:12:in
'>>
更糟糕的是,我无法通过以下方式发现错误:
foo = str.to_ascii rescue 'x'
有谁知道这里发生了什么?
答案 0 :(得分: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所评论。
答案 1 :(得分:1)
rescue clause with no parameter list, the parameter defaults to StandardError;它看起来像unidecoder
引发了另一个异常,但堆栈跟踪似乎不完整(它应该显示异常类型。)