递归地将包含非 UTF 字符的哈希转换为 UTF
我有一个流氓 gem (omniauth),它提供了包含 ASCII-BIT8 字符串的数据哈希,我想将其转换为 UTF。
我怎样才能将散列的所有字符串元素强制转换为UTF,作为某种rails初始化方法? .to_utf8
初始化程序
session[:omniauth] = omniauth.to_utf8
class Hash
def to_utf8
#not really sure what to do here?
end
end
I have a rogue gem (omniauth) which provides a hash of data containing ASCII-BIT8 strings that I would like to convert into UTF.
How can I force all of the string elements of the hash into UTF, as some kind of rails initializer method? .to_utf8
initilizer
session[:omniauth] = omniauth.to_utf8
class Hash
def to_utf8
#not really sure what to do here?
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Ruby 1.9 中,您通常可以使用
encode
方法翻转编码。围绕此问题的包装器可以递归地转换哈希值,这与symbolize_keys
不同,使这一过程变得简单:In Ruby 1.9 you can usually just flip the encoding using the
encode
method. A wrapper around this that recursively transforms the hash, not unlikesymbolize_keys
makes this straightforward:试试这个:
try this: