Encoding error

Hello,

I have an error with this code :

encoding: UTF-8

str=“rép”
puts str.encoding
str=Marshal.dump(str)
puts str.encoding
str=str.encode(‘UTF-8’)
puts str.encoding

when I run this code I obtain :
UTF-8
ASCII-8BIT
test.rb:6:in encode': "\xC3" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError) from test.rb:6:in

I don’t understand where is my fault ?

I use ubuntu 11.10 with echo $LANG=fr_FR.UTF-8

beast regards,

Marshal.dump generates binary data, not containing any real strings
(try “puts str” and see). Therefore trying to encode it will blow up -
either with an immediate error, like it did for you, or it will appear
to work and only corrupt the data…

You can get the string data back by using Marshal.load str.

– Matma R.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs