Last Updated: February 25, 2016
·
472
· csaura

RUBY - quick hash to url query

Got a hash of values you want to convert into a url query string? Use the to_query method:

"http://www.example.com?" + { language: "ruby", status: "awesome"}.to_query
=> "http://www.example.com?language=ruby&status=awesome" 

Want to do it in reverse? Use CGI.parse:

require 'cgi' # Only needed for IRB, Rails already has this loaded
CGI::parse "language=ruby&status=awesome"
=> {"language"=>["ruby"], "status"=>["awesome"]}