utf8 strings and inspect

Discussion in 'Ruby' started by Benny, Jun 4, 2004.

  1. Benny

    Benny Guest

    hi,

    if I have an utf8-string

    utf8str = "äöü"

    then Object#inspect gives me

    utf8str.inspect #=> "\303\244\303\266\303\274"

    but String#to_s gives me

    utf8str.to_s #=> "äöü"

    even if the documentation says that Object#inspect uses .to_s to convert an
    object to a string.

    even funnier, in the irb (I guess that is, because the irb uses
    Object#inspect for the output)

    irb(main):010:0>utf8str.to_s
    => "\303\244\303\266\303\274"
    irb(main):010:0>utf8str.inspect
    => "\"\\303\\244\\303\\266\\303\\274\""

    so how can I change Object#inspect to use String#to_s ?

    any ideas?


    benny
     
    Benny, Jun 4, 2004
    #1
    1. Advertising

  2. Benny

    Carlos Guest

    [Benny <>, 2004-06-04 18.53 CEST]
    > if I have an utf8-string
    >
    > utf8str = "äöü"
    >
    > then Object#inspect gives me
    >
    > utf8str.inspect #=> "\303\244\303\266\303\274"
    >
    > but String#to_s gives me
    >
    > utf8str.to_s #=> "äöü"
    >
    > even if the documentation says that Object#inspect uses .to_s to convert an
    > object to a string.
    >
    > even funnier, in the irb (I guess that is, because the irb uses
    > Object#inspect for the output)
    >
    > irb(main):010:0>utf8str.to_s
    > => "\303\244\303\266\303\274"
    > irb(main):010:0>utf8str.inspect
    > => "\"\\303\\244\\303\\266\\303\\274\""
    >
    > so how can I change Object#inspect to use String#to_s ?


    You don't need to. Just tell ruby that you are working with UTF-8 and not
    ASCII, and it will not escape the non-ASCII bytes in the output.

    irb(main):001:0> utf8str="äöü"
    => "\303\244\303\266\303\274"
    irb(main):002:0> $KCODE='u'
    => "u"
    irb(main):003:0> utf8str
    => "äöü"
    irb(main):004:0> utf8str.inspect
    => "\"äöü\""
     
    Carlos, Jun 4, 2004
    #2
    1. Advertising

  3. Benny

    Benny Guest

    Carlos wrote:

    >
    > You don't need to. Just tell ruby that you are working with UTF-8 and not
    > ASCII, and it will not escape the non-ASCII bytes in the output.

    how can I do that?
     
    Benny, Jun 4, 2004
    #3
  4. Benny

    Carlos Guest

    [Benny <>, 2004-06-04 19.28 CEST]
    > > You don't need to. Just tell ruby that you are working with UTF-8 and not
    > > ASCII, and it will not escape the non-ASCII bytes in the output.

    > how can I do that?


    $KCODE = 'u'
     
    Carlos, Jun 4, 2004
    #4
    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. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    814
    Malcolm
    Jun 24, 2006
  2. deluxstar
    Replies:
    5
    Views:
    894
    Terry Reedy
    Sep 25, 2010
  3. Basile Starynkevitch [news]

    (newbie Q) opposite of inspect for strings

    Basile Starynkevitch [news], May 18, 2005, in forum: Ruby
    Replies:
    10
    Views:
    270
    Joel VanderWerf
    May 21, 2005
  4. gry
    Replies:
    2
    Views:
    826
    Alf P. Steinbach
    Mar 13, 2012
  5. Gary E. Ansok

    UTF8 strings and filesystem access

    Gary E. Ansok, Oct 11, 2007, in forum: Perl Misc
    Replies:
    6
    Views:
    183
    Peter J. Holzer
    Oct 27, 2007
Loading...

Share This Page