In Python, I can do this:
>>> import urlparse, urllib >>> q = urlparse.parse_qsl("a=b&a=c&d=e") >>> urllib.urlencode(q) 'a=b&a=c&d=e'
In Ruby [+ Rails], I cannot figure out how to do the same without โrolling my own,โ which seems strange. The Rails method does not work for me - it adds square brackets to the names of the request parameters that the server at the other end may or may not support:
>> q = CGI.parse("a=b&a=c&d=e") => {"a"=>["b", "c"], "d"=>["e"]} >> q.to_params => "a[]=b&a[]=c&d[]=e"
My use case is just that I want to extinguish the values โโof some values โโin the query-string part of the URL. It was natural to rely on the standard library and / or Rails and write something like this:
uri = URI.parse("http://example.com/foo?a=b&a=c&d=e") q = CGI.parse(uri.query) q.delete("d") q["a"] << "d" uri.query = q.to_params
but only if the resulting URI is actually http://example.com/foo?a=b&a=c&a=d , and not http://example.com/foo?a[]=b&a[]=c&a[]=d . Is there a right or best way to do this?
uri ruby-on-rails hash params
Charlie
source share