0

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: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 yield' 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: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 decode' from 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'>>

What's worse, I can't catch the error by doing:

foo = str.to_ascii rescue 'x'

Does anyone know what's happening here?

pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • What’s your current system `LOCALE`? Regarding rescueing: [rescue clause with no parameter list, the parameter defaults to StandardError](http://rubylearning.com/satishtalim/ruby_exceptions.html); it looks like `unidecoder` raises kinda other exception, but the stacktrace seems to be incomplete (it should show the exception type.) – Aleksei Matiushkin Mar 25 '13 at 04:42
  • @mudasobwa - I think that must be it. I was under the impression that rescue without a parameter caught everything. If you want to put it in an answer I'll accept. – pguardiario Mar 25 '13 at 05:05

2 Answers2

1

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.

Arie Xiao
  • 13,909
  • 3
  • 31
  • 30
1

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.)

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160