How to use umlauts with ruby's URI library

ruby, unicode, uri

Solution

Call URI.encode before parsing:

require 'uri'
uri = URI(URI.encode("http://abc.de/äöü"))

As a side note, be aware that if the URL contains a hash fragment (#something), you'll have to manually split it:

require 'uri'
uri = URI(URI.encode('http://example.com/page.html') + '#' + URI.encode('anchor'))

Problem

Here is the problem: Ruby's URI library does not deal with umlauts; i.e. ``` irb> require "uri" irb* URI("http://abc.de/äöü") URI::InvalidURIError: bad URI(is not URI?): http://abc.de/äöü ``` How can I deal with that? This is Ruby 1.9.2, btw.

Original source