0

我有一个简单的基于机架的 ruby​​ 服务器,我使用 Thin 运行它,它看起来像:

#encoding: utf-8

class Auth

  def initialize(app)
    @app = app
  end

  def call(env)
    @request = Rack::Request.new(env).params
    command = @request['command']

    begin
      command.tr!('٠١٢٣٤٥٦٧٨٩','0123456789')  
    rescue Exception => e
      $LOG.error e.to_s    
    end
  end
end

如您所见,它应该将数字从阿拉伯语翻译成英语,无论如何,当我发送参数命令“١”时,我收到了这个错误:

incompatible character encodings: ASCII-8BIT and UTF-8
4

1 回答 1

1
command("١".force_encoding('UTF-8'))

有关 Ruby 1.9/2.0 中编码的更多详细信息,另请参阅:http: //yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/

于 2013-03-11T18:42:07.057 回答