Escape sequences for unicode chars?

Discussion in 'Ruby' started by Michael Schuerig, Jun 30, 2005.

  1. I'm using unicode in a Rails app and there are some cases where I'd like
    to use escape sequences for characters instead of the literal
    characters.

    Case in point is Horizontal Ellipsis, hex 2026. I thought this would be
    \x2026, but Ruby only takes the first two characters following \x into
    account. Is there some way to make Ruby cooperate?

    Michael

    --
    Michael Schuerig The Fifth Rider of the Apocalypse
    mailto: is a programmer.
    http://www.schuerig.de/michael/
    Michael Schuerig, Jun 30, 2005
    #1
    1. Advertising

  2. On 6/30/05, Michael Schuerig <> wrote:
    > I'm using unicode in a Rails app and there are some cases where I'd like
    > to use escape sequences for characters instead of the literal
    > characters.


    Ruby does not yet support Unicode literals in any way. You will need
    to use the proper UTF-8 encoding bytes as a string. If you can work
    with IConv or something, you might be able to use \x20\x26.

    -austin
    --=20
    Austin Ziegler *
    * Alternate:
    Austin Ziegler, Jun 30, 2005
    #2
    1. Advertising

  3. Michael Schuerig

    Eric Hodel Guest

    On 30 Jun 2005, at 13:15, Michael Schuerig wrote:

    > I'm using unicode in a Rails app and there are some cases where I'd =20=


    > like
    > to use escape sequences for characters instead of the literal
    > characters.
    >
    > Case in point is Horizontal Ellipsis, hex 2026. I thought this =20
    > would be
    > \x2026, but Ruby only takes the first two characters following \x into
    > account. Is there some way to make Ruby cooperate?


    You need to encode it into UTF8 or similar:

    $ ruby -e 'puts "\342\200\246"'
    =85
    $

    --=20
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
    Eric Hodel, Jun 30, 2005
    #3
  4. Eric Hodel wrote:

    > On 30 Jun 2005, at 13:15, Michael Schuerig wrote:


    >> Case in point is Horizontal Ellipsis, hex 2026. I thought this
    >> would be
    >> \x2026, but Ruby only takes the first two characters following \x
    >> into account. Is there some way to make Ruby cooperate?

    >
    > You need to encode it into UTF8 or similar:
    >
    > $ ruby -e 'puts "\342\200\246"'
    > ?
    > $


    Thanks! Somehow I didn't expect this to work. Good that it does. I'm
    using hex "\xe2\x80\xa6" instead as that's what the nice (unixoid)
    unicode command line tool shows, among other things.

    Michael

    --
    Michael Schuerig The Fifth Rider of the Apocalypse
    mailto: is a programmer.
    http://www.schuerig.de/michael/
    Michael Schuerig, Jun 30, 2005
    #4
  5. Michael Schuerig wrote:

    > Thanks! Somehow I didn't expect this to work. Good that it does. I'm
    > using hex "\xe2\x80\xa6" instead as that's what the nice (unixoid)
    > unicode command line tool shows, among other things.



    module Kernel
    def u( str )
    str.gsub(/U\+([0-9a-fA-F]{4,4})/u){["#$1".hex ].pack('U*')}
    end
    end

    >> u"U+2026"

    => "\342\200\246"
    >> "\xe2\x80\xa6"

    => "\342\200\246"

    _why
    why the lucky stiff, Jun 30, 2005
    #5
    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. harrelson
    Replies:
    3
    Views:
    3,871
    Craig Ringer
    Dec 10, 2004
  2. slomo
    Replies:
    5
    Views:
    1,501
    Duncan Booth
    Dec 2, 2007
  3. Guest
    Replies:
    2
    Views:
    548
    Tim Roberts
    Dec 15, 2007
  4. Jeremy
    Replies:
    1
    Views:
    788
    Alex Willmer
    Jan 11, 2011
  5. Jeremy
    Replies:
    0
    Views:
    566
    Jeremy
    Jan 11, 2011
Loading...

Share This Page