有没有解决办法可以使用String.force_encoding()
在 Ruby 1.8.7(或 Rails 2.x)中,以便它像在 Ruby 1.9 中一样工作?我读过一些关于 require 的内容active_support
,但这不起作用
$> 宝石列表--本地| grep 'rails\|activesupport'
activesupport (3.0.3, 2.3.8, 2.3.5)
rails (2.3.8, 2.3.5)
$> ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]
$> rails -v
Rails 2.3.8
irb:
> require "rubygems"
=> true
> require "active_support"
=> true
> "asdf".force_encoding("UTF-8")
NoMethodError: undefined method `force_encoding' for "asdf":String
> String.respond_to?(:force_encoding)
=> false
这将为您提供 Ruby 1.8.7 和 Ruby 1.9 中的 String#to_my_utf8:
require 'iconv'
class String
def to_my_utf8
::Iconv.conv('UTF-8//IGNORE', 'UTF-8', self + ' ')[0..-2]
end
end
进而...
?> "asdf".to_my_utf8
=> "asdf"
灵感来自保罗·巴特利 http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/还记得我以前的一些工作远程表宝石 https://github.com/seamusabshere/remote_table.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)