Skip to content

Instantly share code, notes, and snippets.

@awesome
Created April 1, 2009 06:14
Show Gist options
  • Save awesome/88567 to your computer and use it in GitHub Desktop.
Save awesome/88567 to your computer and use it in GitHub Desktop.
# hash to query string
$ irb
>> hash = {1=>1,2=>2,3=>3}
=> {1=>1, 2=>2, 3=>3}
>> hash.to_a
=> [[1, 1], [2, 2], [3, 3]]
>> hash.to_a.map {|x| x.join("=")}.join("&")
=> "1=1&2=2&3=3"
# query string to hash
# http://www.ruby-doc.org/core-1.8.4/classes/CGI.html
# http://refactormycode.com/codes/359-query-string-to-hash
# reima, No need to reimplement functionality already provided by the Standard Library:
require 'cgi'
uri = URI.parse("http://www.mydomain.com/search?q=foo&bar=stick&who=you")
uri_params = CGI.parse(uri.query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment