Q:

How to change URL parameters through a loop using Ruby?

I am trying to change each parameter of this http://www.exampleofurl.com/search?q=4&e=25;i=8&b=2 through a loop, but I am getting this output.
http://www.exampleofurl.com/search?q=changed&e=25;i=8&b=2
http://www.exampleofurl.com/search?q=4&e=changed;i=8&b=2
http://www.exampleofurl.com/search?q=4&e=25;i=changed&b=2
http://www.exampleofurl.com/search?q=4&e=changed5;i=8&b=changed

When my parameters are empty, I get this weird output.
search?changedqchanged=changed4changed&changedechanged=changed2changed5changed;changedichanged=changed8changed&changedbchangedochangedochanged=changed

Here is my code.
require 'uri'
url = "http://www.exampleofurl.com/search?q=4&e=25;i=8&b=2"
uri = URI.parse(url)

params = {}
t = "changed"

q = uri.query
p = q.split(/[&;]/) if q != nil

p.each do |part|
   k,v = part.split('=', 2)
   params[k] = v

   t.each do |tr|
       uri.query = q.gsub(params[k], tr)
   end
end

Can someone tell me what is wrong and what is the most efficient way to do it?
I want an output like this.
q=changed&e=25;i=8&b=2
q=4&e=changed;i=8&b=2
q=4&e=25;i=changed&b=2
q=4&e=25;i=8&b=changed

Thanks in advance.
Marco

A:

for working on the query part, i think that you could do something like this:
CGI::parse(uri.query).inject({}) { |hash, (key, value)| hash[key] = "changed"; hash}

this should result in a hash like
{"q"=>"changed", "e"=>"changed", "i"=>"changed", "b"=>"changed"}

if you join this back into your parsed URI, you should be done.

