Flackou Flackou - 1 year ago 102
Ruby Question

How to extract URL parameters from a URL with Ruby or Rails?

I have some URLs, like


and I would like to extract the parameters from these URLs and get them in a Hash. Obviously, I could use regular expressions, but I was just wondering if there was easier ways to do that with Ruby or Rails. I haven't found anything in the Ruby module
but perhaps I missed something.

In fact, I need a method that would do that :

#=> {:param1 => 'value1', :param2 => 'value2', :param3 => 'value3'}

Would you have some advices? Thanks in advance.


Answer Source

I think you want to turn any given URL string into a HASH?

You can try http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075

require 'cgi'



{"param1"=>["value1"], "param2"=>["value2"], "param3"=>["value3"]}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download