Search code examples
rubyrackthin

Incompatible character encodings error when I send arabic number to my ruby rack server


I have a simple rack based ruby server that I run using Thin, it looks something like :

#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

As you can see, it is supposed to translate numbers from arabic to english, anyways when I send param command "١" I'm getting this error :

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

Solution

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

    See also for more detail on encoding in Ruby 1.9/2.0: http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/