Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"\xC2" to UTF-8 in conversion from ASCII-8BIT to UTF-8

I have a rails project that runs fine with MRI 1.9.3. When I try to run with Rubinius I get this error in app/views/layouts/application.html.haml:

"\xC2" to UTF-8 in conversion from ASCII-8BIT to UTF-8

like image 368
alf Avatar asked Apr 25 '13 15:04

alf


1 Answers

It turns out the page had an invalid character (an interpunct '·'), which I found out with the following code (credits to this gist and this question):

lines = IO.readlines("app/views/layouts/application.html.haml").map do |line|
  line.force_encoding('ASCII-8BIT').encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?')
end

File.open("app/views/layouts/application.html.haml", "w") do |file|
  file.puts(lines)
end

After running this code, I could find the problematic characters with a simple git diff and moved the code to a helper file with # encoding: utf-8 at the top. I'm not sure why this doesn't fail with MRI but it should since I'm not specifying the encoding of the haml file.

like image 123
alf Avatar answered Nov 15 '22 01:11

alf