Last active
June 26, 2017 13:27
-
-
Save af23me/1a175c4e1b17517e84b266739f301e89 to your computer and use it in GitHub Desktop.
Deep encode hash
This file contains 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
def encode_hash(hashie) | |
Hash[ | |
hashie.collect do |k, v| | |
if (v.is_a?(Hash)) | |
[ k, encode_hash(v) ] | |
elsif (v.respond_to?(:encoding)) | |
[ k, v.dup.force_encoding('UTF-8').encode('UTF-16', {invalid: :replace, undef: :replace, replace: '?'}).encode('UTF-8') ] | |
elsif (v.respond_to?(:to_utf8)) | |
[ k, v.to_utf8 ] | |
else | |
[ k, v ] | |
end | |
end | |
] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment