On Wed, 15 Mar 2006, ngw wrote: > Hi *, my question is really simple: is there a way in Ruby stdlib or in > external modules to build querystrings without creating the string from > scratch ? > For example something like: > > name = "Foo Bar" > p URI::join(base_uri, query_string(name)) > > -> "http://foobar.org/?name=Foo+Bar" > > Obviously this code is totally invented. > If not (haven't found anything in the documentation) how does people > accomplish this task ? > > TIA, > ngw harp:~ > cat a.rb require "cgi" class Hash def query_string "?" << inject([]){|a,kv| a << [CGI.escape(kv.shift), CGI.escape(kv.shift)].join("=") }.join("&") end end q = { "name" => "Foo Bar", "uri" => "http://ruby-lang.org" } p q.query_string harp:~ > ruby a.rb "?name=Foo+Bar&uri=http%3A%2F%2Fruby-lang.org" regards. -a -- share your knowledge. it's a way to achieve immortality. - h.h. the 14th dali lama