american-ninja-warrior
american-ninja-warrior

Reputation: 8185

how to fix Encoding::UndefinedConversionError in ruby

When trying to write a string to a file I get this message:

irb(main):011:0> IO.write("/tmp/a1", r1.body.to_s)
Encoding::UndefinedConversionError: "\xC2" from ASCII-8BIT to UTF-8
    from (irb):11:in `write'
    from (irb):11
irb(main):012:0> 

What am I doing wrong?

Upvotes: 1

Views: 1302

Answers (1)

Paulo Felipe Souza
Paulo Felipe Souza

Reputation: 386

I found a question like yours. Your string is in some other encoding, most likely iso-8859-1, so you should run this to convert it:

"\xC2".encode("iso-8859-1").force_encoding("utf-8") => "Ã"

See the original question on stackoverflow, the answer on the top right now seem to be usefull.

Upvotes: 2

Related Questions