Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use umlauts with ruby's URI library

Tags:

uri

ruby

unicode

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.

like image 505
radiospiel Avatar asked May 09 '12 15:05

radiospiel


1 Answers

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'))
like image 158
tokland Avatar answered Sep 23 '22 14:09

tokland