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

encoding, haml, rubinius, ruby-on-rails

Solution

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.

Problem

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

Original source