On 29.07.2011 01:51, Tyler wrote:
> Hi all,
> I'm trying to parse escaped unicode characters. The basic goal is to
> read the string '\u00F3' (or "\\u00F3") as 'ó'. I have a workaround
> below that uses eval (code below), but I'd be grateful if anyone had a
> less dangerous solution or suggestion. In python, you can 'import
> codecs' and use string.decode("unicode-escape"), is something similar
> possible in Ruby?
irb(main):037:0> s="a\\u00fab"
=> "a\\u00fab"
irb(main):038:0> puts s
a\u00fab
=> nil
irb(main):039:0> s.gsub(%r[\\u(\h{4})]) {$1.to_i(16).chr(Encoding::UTF_

}
=> "aúb"
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/