Resolving unicode escapes to unicode character

Discussion in 'Ruby' started by Tyler, Jul 29, 2011.

  1. Tyler

    Tyler Guest

    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?

    Thanks!
    Tyler


    File.open("test.txt", 'w') {|file| file.puts "Asociaci\\u00F3n Alumni
    \nF\\u00FAtbol"}
    File.open "test.txt", 'r' do |file|
    file.each do |line|
    puts eval("%Q{#{line}}")
    # puts line
    end
    end
    # => Asociación Alumni
    # => Fútbol
    #
    # If 'puts line' is used instead, this is the output:
    # => Asociaci\u00F3n Alumni
    # => F\u00FAtbol
    #
    # Is there a (prettier & safer) way to do this without using eval?
     
    Tyler, Jul 29, 2011
    #1
    1. Advertising

  2. 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_8)}
    => "aúb"

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jul 29, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olav
    Replies:
    0
    Views:
    351
  2. Olav
    Replies:
    1
    Views:
    421
    Martin Honnen
    Aug 11, 2004
  3. Rick Brandt

    Escapes Sequences Not Working?

    Rick Brandt, Aug 25, 2004, in forum: XML
    Replies:
    13
    Views:
    742
    Donald Roby
    Aug 28, 2004
  4. David J Birnbaum

    overriding character escapes during file input

    David J Birnbaum, Sep 3, 2006, in forum: Python
    Replies:
    2
    Views:
    337
    John Machin
    Sep 3, 2006
  5. Knute Johnson

    Unicode escapes and String literals?

    Knute Johnson, Dec 13, 2012, in forum: Java
    Replies:
    24
    Views:
    645
    Roedy Green
    Dec 17, 2012
Loading...

Share This Page