865351a8b8bb963c7ee935b3f342854e

I'm looking if someone can parse a HTTP query string in the shortest possible way.

q = "query1=apa&query2=apa2"
Hash[q.split("&").collect{|a| a.split("=")}.flatten]
=> {"query1"=>"apa", "query2"=>"apa2"}

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

October 5, 2009, October 05, 2009 20:18, permalink

2 ratings. Login to rate!
require'cgi';CGI.parse(q)
F4192eeb4b26e96deab8b5c68926105d

Muke Tever

October 6, 2009, October 06, 2009 06:29, permalink

2 ratings. Login to rate!

And if for any reason you don't want to do require 'cgi', this is a shorter way of doing what you were doing:

Hash[*q.split(/=|&/)]

Your refactoring





Format Copy from initial code

or Cancel