我如何在Ruby上执行此操作?
puts some_method("ò")
# => "ò"
换句话说,将重音字符ò
转换为HTML版本:ò
我试过这样:
# coding: utf-8
require 'rubygems'
require 'htmlentities'
require 'unicode'
coder = HTMLEntities.new
string = "Scròfina"
puts coder.encode(string, :named)
但是我得到了这个(来自:http://htmlentities.rubyforge.org/):
/Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:85:in `unpack': malformed UTF-8 character (expected 2 bytes, given 1 bytes) (ArgumentError)
from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:85:in `encode_decimal'
from (eval):2:in `encode_extended'
from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `encode'
from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `gsub!'
from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `encode'
from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities.rb:74:in `encode'
from unicode_pleasure.rb:8
感谢您的时间!
答案 0 :(得分:12)
我已明确设置$ KCODE以使您的示例正常工作。另外,请确保您的源文件实际编码为UTF-8 !
# coding: utf-8
require 'rubygems'
require 'htmlentities'
require 'unicode'
$KCODE = 'UTF-8'
coder = HTMLEntities.new
string = "Scròfina"
puts coder.encode(string, :named)