Utf8 strings and inspect

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 <linux@marcrenearns.de>, 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 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 <linux@marcrenearns.de>, 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'