# Building querystrings with ruby

**URL:** <https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890>\
**Category:** Ruby\
**Created:** [March 15, 2006, 1:27am UTC](https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890 "2006-03-15T01:27:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@ngw](https://www.ruby-forum.com/u/ngw)\
**Post date:** [March 15, 2006, 1:27am UTC](https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890/1 "2006-03-15T01:27:18Z")

</div>

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 B.”  
p URI::join(base\_uri, query\_string(name))

→ “[Network Ability Ltd - Internet Systems Consultancy](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

---

<div class="post-metadata">

**Author:** ![ngw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@ngw](https://www.ruby-forum.com/u/ngw)\
**Post date:** [March 15, 2006, 3:35am UTC](https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890/2 "2006-03-15T03:35:00Z")

</div>

On Wed, 15 Mar 2006, ngw wrote:

> 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 B.”, “uri” =\> “[http://ruby-lang.org](http://ruby-lang.org)” }

p q.query\_string

harp:~ \> ruby a.rb  
“?name=Foo+Bar&uri=http%3A%2F%[2Fruby-lang.org](http://2Fruby-lang.org)”

regards.

-a

---

<div class="post-metadata">

**Author:** ![ngw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@ngw](https://www.ruby-forum.com/u/ngw)\
**Post date:** [March 15, 2006, 10:09am UTC](https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890/3 "2006-03-15T10:09:25Z")

</div>

On 15/03/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require “cgi”  
> class Hash  
> def query\_string  
> “?” \<\< inject([]){|a,kv| a \<\< [CGI.escape(kv.shift), CGI.escape(kv.shift)].join(“=”) }.join(“&”)  
> end  
> end

Now here’s somewhere I can use the handy trick I learned yesterday:

“?” \<\< inject([]){ |a,(k,v)| a \<\< [CGI.escape(k),  
CGI.escape(v)].join(“=”) }.join(“&”)

But this is shorter:

“?” \<\< inject([]){ |a,kv| a \<\< kv.map{ |e| CGI.escape(e) }.join(“=”)  
}.join(“&”)

And this is shorter still:

“?” \<\< to\_hash.map{ |kv| kv.map{ |e| CGI.escape(e) }.join(“=”)  
}.join(“&”)

Paul.

---

<div class="post-metadata">

**Author:** ![ngw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@ngw](https://www.ruby-forum.com/u/ngw)\
**Post date:** [March 15, 2006, 11:20am UTC](https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890/4 "2006-03-15T11:20:34Z")

</div>

-Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On 15/03/06, Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > And this is shorter still:
> > 
> > “?” \<\< to\_hash.map{ |kv| kv.map{ |e| CGI.escape(e) }.join(“=”) }.join(“&”)
> 
> Ignore my stupidity above; this is shorter and less redundant:
> 
> “?” \<\< map{ |kv| kv.map{ |e| CGI.escape(e) }.join(“=”) }.join(“&”)

Wow !  
Thank you both, great snippets.

ngw

---

<div class="post-metadata">

**Author:** ![ngw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@ngw](https://www.ruby-forum.com/u/ngw)\
**Post date:** [March 16, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890/5 "2006-03-16T15:10:19Z")

</div>

Wow, I have no idea what this does or how it works. Would you mind  
taking this apart and describing how it works?

---

<div class="post-metadata">

**Author:** ![ngw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@ngw](https://www.ruby-forum.com/u/ngw)\
**Post date:** [March 15, 2006, 10:12am UTC](https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890/6 "2006-03-15T10:12:31Z")

</div>

On 15/03/06, Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And this is shorter still:
> 
> “?” \<\< to\_hash.map{ |kv| kv.map{ |e| CGI.escape(e) }.join(“=”) }.join(“&”)

Ignore my stupidity above; this is shorter and less redundant:

“?” \<\< map{ |kv| kv.map{ |e| CGI.escape(e) }.join(“=”) }.join(“&”)

Paul.

---

<div class="post-metadata">

**Author:** ![ngw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@ngw](https://www.ruby-forum.com/u/ngw)\
**Post date:** [March 22, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890/7 "2006-03-22T16:34:54Z")

</div>

perfectly. very clever.

thanks,

chad.

---

<div class="post-metadata">

**Author:** ![ngw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@ngw](https://www.ruby-forum.com/u/ngw)\
**Post date:** [March 16, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/building-querystrings-with-ruby/54890/8 "2006-03-16T17:28:21Z")

</div>

On 16/03/06, ebeard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Wow, I have no idea what this does or how it works. Would you mind  
> taking this apart and describing how it works?

“?”\<\<map{|h|h.map{|e|CGI.escape(e)}_‘=’}_‘&’

This can be spaced out a bit:

“?” \<\< # append what follows to the string ‘?’  
map { |h| # h looks like [‘key’, ‘value’] for each key-value  
pair  
h.map { |e| # e looks like ‘key’ or ‘value’  
CGI.escape(e) # escape each element  
} \* ‘=’ # join key and value with ‘=’  
} \* ‘&’ # join each key-value pair with ‘&’

Is that any clearer?

Paul.
