Skip to content

Instantly share code, notes, and snippets.

@af23me
Last active June 26, 2017 13:27
Show Gist options
  • Save af23me/1a175c4e1b17517e84b266739f301e89 to your computer and use it in GitHub Desktop.
Save af23me/1a175c4e1b17517e84b266739f301e89 to your computer and use it in GitHub Desktop.
Deep encode hash
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