I am trying to run a ruby script that uses inside a function that removes diacritics:
def remove_diacritics(text)
return text.tr!(
"ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz")
end
I have the magic # encoding: utf-8 at the beginning and I am getting some strange behaviour. It works on Mac but when I copy exactly the same file to Raspberry Pi, I am getting this error:
remove_diacritics.rb:28:in `tr!’: incompatible character encodings: US-ASCII and UTF-8 (Encoding::CompatibilityError)
This seems to be a classic in help forums. The mesmerising thing is that it does work on one machine and doesn’t on the other one, even though the versions of Ruby are exactly the same, ruby 1.9.3p286 (2012-10-12 revision 37165).
Any suggestions?
It’s as Frederick points out. The
(en)coding:comment determines the source encoding, but the error is caused by a mismatch with the external encoding. Your code produces CompatibilityError when run asbut works with either of these
See Ruby 1.9’s Three Default Encodings, a part of a great series explaining the Unicode situation in Ruby 1.8 and 1.9.