问题
I have some content from feeds. In these feeds, UTF-8 characters are often encoded as character references, ie "å" is "å". To avoid double encoding these in my views (ie "å") I want to convert these back to normal UTF_8 characters. How can I do this in Ruby?
I want:
"å".convert_to_utf8 => "å"
回答1:
The HTMLEntities gem is designed to do just this.
require 'htmlentities'
coder = HTMLEntities.new
string = "élan"
coder.decode(string) # => "élan"
# or
string.decode_entities # => "élan"
来源:https://stackoverflow.com/questions/958361/how-can-i-convert-character-references-to-utf-8-strings-in-ruby