How to construct URI with query arguments from hash in Ruby

If you have ActiveSupport, just call '#to_query' on hash.

hash = {
  a: 'argument1',
  b: 'argument2'
  #... dozen more arguments
}
URI::HTTPS.build(host: 'example.com', query: hash.to_query)

=> https://example.com?a=argument1&b=argument2

If you are not using rails remember to require 'uri'


For those not using Rails or Active Support, the solution using the Ruby standard library is

hash = {
  a: 'argument1',
  b: 'argument2'
}
URI::HTTPS.build(host: 'example.com', query: URI.encode_www_form(hash))
=> #<URI::HTTPS https://example.com?a=argument1&b=argument2>