Skip to content

Instantly share code, notes, and snippets.

@dylancashman
Created October 7, 2013 14:38
Show Gist options
  • Save dylancashman/6869084 to your computer and use it in GitHub Desktop.
Save dylancashman/6869084 to your computer and use it in GitHub Desktop.
Utf8Cleaner for encoding problems.
module Utf8Cleaner
def to_utf8(new_value)
if new_value.is_a? String
begin
# Try it as UTF-8 directly
new_value.force_encoding('UTF-8')
unless new_value.valid_encoding?
# Some of it might be old Windows code page
new_value.encode!( 'UTF-8', 'Windows-1252' )
end
rescue EncodingError
# Force it to UTF-8, throwing out invalid bits
new_value.encode!( 'UTF-8', invalid: :replace, undef: :replace )
end
end
new_value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment