Last active
December 31, 2015 05:49
-
-
Save Malet/7942995 to your computer and use it in GitHub Desktop.
Prevent invalid UTF-8 byte sequences for Ruby 2.0 / 2.1 (1.9 untested)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
REPLACE_INVALID_WITH = '�' | |
def to_utf8 | |
self.encode('utf-8', invalid: :replace, undef: :replace, replace: REPLACE_INVALID_WITH). | |
chars.map{ |i| i.valid_encoding? ? i : REPLACE_INVALID_WITH }.join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment