Created
January 19, 2012 12:39
-
-
Save np422/1639893 to your computer and use it in GitHub Desktop.
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
def reencode_session(data) | |
if data.is_a? Hash | |
result = {} | |
data.map do |key,val| | |
if val.is_a?(Hash) || val.is_a?(Array) | |
result[key] = reencode_session(val) | |
else | |
result[key] = ensure_utf8(val) | |
end | |
end | |
result | |
elsif data.is_a? Array | |
data.map do |val| | |
if val.is_a?(Hash) || val.is_a?(Array) | |
reencode_session(val) | |
else | |
ensure_utf8(val) | |
end | |
end | |
end | |
end | |
def ensure_utf8(val) | |
if val.nil? | |
nil | |
else | |
if val.respond_to?(:encoding) && val.respond_to?(:encode) | |
val.encoding != ::Encoding::UTF_8 ? val.encode(::Encoding::UTF_8) : val | |
else | |
val | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment