# URL format

**URL:** <https://www.ruby-forum.com/t/url-format/175515>\
**Category:** Ruby\
**Created:** [September 15, 2009, 2:58am UTC](https://www.ruby-forum.com/t/url-format/175515 "2009-09-15T02:58:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rereis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rereis](https://www.ruby-forum.com/u/rereis)\
**Post date:** [September 15, 2009, 2:58am UTC](https://www.ruby-forum.com/t/url-format/175515/1 "2009-09-15T02:58:43Z")

</div>

I have the following hash myhash  
{ “inter” =\> “team”, “shirt” =\> “stripe” }

desired output

inter=team&shirt=stripe

I tried url\_for myhash but got undefined method `url\_for’ for

thanks

---

<div class="post-metadata">

**Author:** ![rereis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rereis](https://www.ruby-forum.com/u/rereis)\
**Post date:** [September 15, 2009, 3:33am UTC](https://www.ruby-forum.com/t/url-format/175515/2 "2009-09-15T03:33:07Z")

</div>

On Mon, Sep 14, 2009 at 5:58 PM, Re BR [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have the following hash myhash  
> { “inter” =\> “team”, “shirt” =\> “stripe” }
> 
> desired output
> 
> inter=team&shirt=stripe
> 
> I tried url\_for myhash but got undefined method `url\_for’ for

The single line answer would be

hsh = { “inter” =\> “team”, “shirt” =\> “stripe” }  
hsh.to\_a.map{|e| e.join(‘=’)}.join(‘&’) # ‘inter=team&shirt=stripe’

Broken down

hsh = { “inter” =\> “team”, “shirt” =\> “stripe” }  
ary = hsh.to\_a #[[‘inter’, ‘team’], [‘shirt’, ‘stripe’]] - take the hash  
and  
switch into array  
new\_ary = ary.map{|e| e.join(‘=’)} #[‘inter=team’, ‘shirt=‘stripe’] -  
take  
the array and join the inner arrays using a =  
final = new\_ary.join(’&') # ‘inter=team&shirt=stripe’ take the remaining  
array and join the element with &

John

---

<div class="post-metadata">

**Author:** ![rereis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rereis](https://www.ruby-forum.com/u/rereis)\
**Post date:** [September 15, 2009, 3:40am UTC](https://www.ruby-forum.com/t/url-format/175515/3 "2009-09-15T03:40:44Z")

</div>

Hi –

On Tue, 15 Sep 2009, John W Higgins wrote:

> > 
> 
> The single line answer would be
> 
> hsh = { “inter” =\> “team”, “shirt” =\> “stripe” }  
> hsh.to\_a.map{|e| e.join(’=’)}.join(’&’) # ‘inter=team&shirt=stripe’

No need for the to\_a. You’ll get a [key,value] array in e each time  
through by just mapping the hash.

Note that the order might come out different; for example, on my  
machine anyway:

> > hsh = { “inter” =\> “team”, “shirt” =\> “stripe” }  
> > =\> {“shirt”=\>“stripe”, “inter”=\>“team”}
> 
> > hsh.map {|entry| entry.join("=") }.join("&")  
> > =\> “shirt=stripe&inter=team”

though I suspect it doesn’t matter in this case.

David

---

<div class="post-metadata">

**Author:** ![rereis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rereis](https://www.ruby-forum.com/u/rereis)\
**Post date:** [September 15, 2009, 5:15am UTC](https://www.ruby-forum.com/t/url-format/175515/4 "2009-09-15T05:15:32Z")

</div>

Hi,

Am Dienstag, 15. Sep 2009, 10:32:24 +0900 schrieb John W Higgins:

> hsh = { “inter” =\> “team”, “shirt” =\> “stripe” }  
> hsh.to\_a.map{|e| e.join(’=’)}.join(’&’) # ‘inter=team&shirt=stripe’

The correct way escapes the `=' and`&’ signs. I wrote my own  
library and it seems that it doesn’t. Ugh.

Bertram

---

<div class="post-metadata">

**Author:** ![rereis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rereis](https://www.ruby-forum.com/u/rereis)\
**Post date:** [September 15, 2009, 5:10am UTC](https://www.ruby-forum.com/t/url-format/175515/5 "2009-09-15T05:10:51Z")

</div>

At 2009-09-14 09:40PM, “David A. Black” wrote:

> > hsh = { “inter” =\> “team”, “shirt” =\> “stripe” }  
> > hsh.to\_a.map{|e| e.join(’=’)}.join(’&’) # ‘inter=team&shirt=stripe’
> 
> > > hsh = { “inter” =\> “team”, “shirt” =\> “stripe” }  
> > > hsh.map {|entry| entry.join("=") }.join("&")  
> > > =\> “shirt=stripe&inter=team”

I’m surprised that the URI module doesn’t have a method to handle it.

---

<div class="post-metadata">

**Author:** ![rereis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rereis](https://www.ruby-forum.com/u/rereis)\
**Post date:** [September 15, 2009, 5:21am UTC](https://www.ruby-forum.com/t/url-format/175515/6 "2009-09-15T05:21:59Z")

</div>

Hi,  
I think I had faced the same issue before for encoding, I have used this  
method for encoding urls.

require ‘rubygems’  
require ‘rest-open-uri’  
require ‘uri’  
require ‘cgi’

def form\_encoded(hash)  
encoded = []  
hash.each do |key, value|  
encoded \<\< CGI.escape(key) + ‘=’ + CGI.escape(value)  
end  
return encoded.join(’&’)  
end

representation = form\_encoded({ “user[name]” =\> username,  
“user[password]” =\>  
password,  
“user[full\_name]”  
=\>  
full\_name,  
“user[email]” =\>  
email  
})

hope it will work for you…

## –

Thanks and Regards  
Saurabh P.  
+91-9922071155  
skype: sorab\_pune  
yahoo & gtalk: saurabh.purnaye  
msn: removed\_email\_address@domain.invalid

–  
Please don’t print this e-mail unless you really need to.

On Tue, Sep 15, 2009 at 8:44 AM, Bertram S.
