TIL: An almost pretty way to build a URL in ruby

I've been generally disheartened by Ruby's URI handling, it's better than most, but still lacking as most of Ruby's standard lib is. I've finally decided on my preferred way to build a URL with query parameters by using Ruby's URI.encode_www_form and a basic hash.

  url = URI.parse("https://waters.today/api/v5/search/")
  query = {q: query, mkt: market}
  query[:count] = count unless count.nil?
  query[:offset] = offset unless offset.nil?
  query[:safesearch] = safesearch? unless savesearch?.nil?
  url.query = URI.encode_www_form(query)

Additionally you can use URI.decode_www_form to parse a existing URI parameters from url.query, modify them, and append them to back url.query << URI.encode_www_form(foo)