Reputation: 55002
I'm trying out the unidecoder gem and it's giving me problems with some strings:
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): found unknown escape character while parsing a quote d scalar at line 2 column 3 from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in
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:inparse' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:127:in
load' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:inblock in load_file' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:in
open' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:inload_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:inyield' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in
default' from 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
block in decode' from 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
decode' from 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
'>>
What's worse, I can't catch the error by doing:
foo = str.to_ascii rescue 'x'
Does anyone know what's happening here?
Upvotes: 0
Views: 566
Reputation: 121010
rescue clause with no parameter list, the parameter defaults to StandardError; it looks like unidecoder
raises kinda other exception, but the stacktrace seems to be incomplete (it should show the exception type.)
Upvotes: 1
Reputation: 14082
Take a look at "C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder/data/x00.yml". Line 2 is an YAML entry - "\z"
,which is not a valid escape sequence in Ruby(but a Regexp anchor to mark the end of string). This might be a bug. You can edit this line to - "\x00"
.
However, "\u00A3"
(£) is not a valid ASCII character, I didn't find the point of encoding it to ASCII.
The exception raised is Psych::SyntaxError, you can catch that specific exception, as @mudasobwa commented.
Upvotes: 1