Skip to content

Instantly share code, notes, and snippets.

@sonkm3
Last active December 20, 2015 08:49
Show Gist options
  • Save sonkm3/6102865 to your computer and use it in GitHub Desktop.
Save sonkm3/6102865 to your computer and use it in GitHub Desktop.
ruby: hash to query string
def get_query_string(query_hash)
query_array = []
query_hash.each{|key, val| val ? query_array.push([key, val].join('=')) : nil}
query_string = query_array != [] ? query_array.join('&') : ''
end
query_hash = {"a"=> 1, "c" =>nil, "b"=>3}
p get_query_string(query_hash)
# "a=1&b=3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment