0

unidecoder gemを試していますが、いくつかの文字列に問題があります。

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):2行目で引用符dスカラーを解析中に不明なエスケープ文字が見つかりました列3fromC:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in parse' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:inparse_stream'from C:/Ruby193/lib/ruby/1.9.1/psych.rb:151:in parse' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:127:inload' from C :/Ruby193/lib/ruby/1.9.1/psych.rb:297:in block in load_file' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:inopen'from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:in load_file' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:8:in block in' from C:/Ruby193 /lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in default'from yield' 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:in decode_char' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:39:in block in decode'from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:37:in 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:in to_ascii' from (irb):21 from C:/Ruby193/bin/irb:12:in' >>

さらに悪いことに、次のようにしてエラーをキャッチすることはできません。

foo = str.to_ascii rescue 'x'

ここで何が起こっているのか誰か知っていますか?

4

2 に答える 2

1

「C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder/data/x00.yml」をご覧ください。2行目はYAMLエントリ- "\z"であり、Rubyでは有効なエスケープシーケンスではありません(ただし、文字列の終わりを示す正規表現アンカー)。これはバグの可能性があります。この行をに編集できます- "\x00"

ただし、"\u00A3"(£)は有効なASCII文字ではないため、ASCIIにエンコードする意味がわかりませんでした。

発生した例外はPsych::SyntaxErrorです。@mudasobwaがコメントしたように、その特定の例外をキャッチできます。

于 2013-03-25T04:56:29.277 に答える
1

パラメータリストのないrescue句。パラメータのデフォルトはStandardErrorです。unidecoder他の例外が発生するように見えますが、スタックトレースが不完全なようです(例外タイプが表示されるはずです)。

于 2013-03-25T05:06:43.707 に答える