Skip to content

Instantly share code, notes, and snippets.

@ogijun
Created February 20, 2012 12:48
Show Gist options
  • Save ogijun/1869060 to your computer and use it in GitHub Desktop.
Save ogijun/1869060 to your computer and use it in GitHub Desktop.
Guessing the encoding of given string by trying only Japanese major encodings.
def ugly_guess_jp str
%w[UTF-8 EUC-JP Shift_JIS eucJP-ms Windows-31J].find do |encoding|
begin
tested = str.force_encoding(encoding)
tested.valid_encoding? && tested.encode('UTF-8').valid_encoding?
rescue Encoding::UndefinedConversionError => e
nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment