Skip to content

Instantly share code, notes, and snippets.

@benhoskings
Last active December 27, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhoskings/7241823 to your computer and use it in GitHub Desktop.
Save benhoskings/7241823 to your computer and use it in GitHub Desktop.
class String
def normalize
mb_chars.normalize(:kd).gsub(/[^\x00-\x7f]/n, '').to_s
end
def normalize_for_display
mb_chars.normalize(:kd).
gsub('æ', 'ae').
gsub('ð', 'd').
gsub(/[^\x00-\x7f]/n, '').to_s
end
end
class String
def to_utf8
if valid_encoding?
force_encoding("utf-8")
else
force_encoding("utf-8").encode("utf-16be", :invalid => :replace, :replace => "?").encode("utf-8")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment