如何在Ruby中将字符引用转换为UTF-8字符串?

时间:2009-06-05 22:04:49

标签: ruby utf-8 character

我有一些来自Feed的内容。在这些馈送中,UTF-8字符通常被编码为字符引用,即“å”是“å”。为了避免在我的视图中对这些进行双重编码(即“& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp我怎么能在Ruby中做到这一点?

我想:

“å”。convert_to_utf8 => “A”

1 个答案:

答案 0 :(得分:6)

HTMLEntities gem旨在实现这一目标。

require 'htmlentities'
coder = HTMLEntities.new
string = "élan"
coder.decode(string) # => "élan"
# or
string.decode_entities # => "élan"