utf8 strings and inspect

B

Benny

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
 
C

Carlos

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
=> "\"äöü\""
 
B

Benny

Carlos said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top