Search code examples
ruby-on-railsrubyencodingiso-8859-1

Ruby equivalent to PHPs urlencode


I need to convert a URL in Ruby which contains the character "ö".

In PHP, urlencode returns %F6 for ö which seems to be the Hex-value for "ö" in ISO 8859.

I tried several different methods but none of them returned the correct character:

  • CGI.escape 'ö' -> %C3%B6
  • URI.encode 'o' -> %C3%B6
  • ERB::Util.url_encode 'ö' -> %C3%B6
  • 'ö'.force_encoding('iso-8859-1') -> \xC3\xB

What method should I use to get the desired output?

-e-

Additional requirement:

I only need to convert these characters in the path of the url. Colons, slashes etc. should remain the same:

http://example.com/this/is/an/ö

will be

http://example.com/this/is/an/%F6


Solution

  • Ruby uses UTF-8 strings by default:

    str = 'ö'
    
    str.encoding
    #=> #<Encoding:UTF-8>
    

    If you want an ISO 8859 encoded string in Ruby, you have to convert it:

    str.encode('ISO-8859-1')
    #=> "\xF6"
    

    to URL-encode a string, there's CGI.escape:

    require 'cgi'
    
    CGI.escape(str.encode('ISO-8859-1'))
    #=> "%F6"
    

    to encode an URL, use URI.escape:

    require 'uri'
    
    url = 'http://example.com/this/is/an/ö'
    URI.escape(url.encode('ISO-8859-1'))
    #=> "http://example.com/this/is/an/%F6"