gpt4 book ai didi

ruby-on-rails - Ruby 1.8.7(或 Rails 2.x)中的 String.force_encoding()

转载 作者:数据小太阳 更新时间:2023-10-29 06:38:36 25 4
gpt4 key购买 nike

是否有在 Ruby 1.8.7(或 Rails 2.x)中使用 String.force_encoding() 的解决方案,以便它像在 Ruby 1.9 中一样工作?我读了一些关于 require active_support 的内容,但这不起作用

$> gem 列表--本地 | 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"

灵感来自 Paul Battley还记得我以前在 remote_table gem 上的一些工作.

关于ruby-on-rails - Ruby 1.8.7(或 Rails 2.x)中的 String.force_encoding(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4583924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com