Query strings carry small name/value pairs after the ? in a URL.

query params `URI.decode_www_form` decodes a query string into pairs that can be turned into a hash.

Query Parameters

raw_query
query_parameters.rb
Replay: real traced execution (multi-file project)
require "uri"

raw_query = "page=2&sort=name"
pairs = URI.decode_www_form(raw_query)
params = pairs.to_h

first_key = pairs.first[0]
first_value = pairs.first[1]
tag_count = pairs.count { |key, _value| key == "tag" }

puts "raw_query=#{raw_query}"
puts "first=#{first_key}:#{first_value}"
puts "sort=#{params.fetch('sort', 'none')}"
puts "tag_count=#{tag_count}"
require "uri"

raw_query = "page=1&sort=date"
pairs = URI.decode_www_form(raw_query)
params = pairs.to_h

first_key = pairs.first[0]
first_value = pairs.first[1]
tag_count = pairs.count { |key, _value| key == "tag" }

puts "raw_query=#{raw_query}"
puts "first=#{first_key}:#{first_value}"
puts "sort=#{params.fetch('sort', 'none')}"
puts "tag_count=#{tag_count}"
require "uri"

raw_query = "tag=ruby&tag=trace"
pairs = URI.decode_www_form(raw_query)
params = pairs.to_h

first_key = pairs.first[0]
first_value = pairs.first[1]
tag_count = pairs.count { |key, _value| key == "tag" }

puts "raw_query=#{raw_query}"
puts "first=#{first_key}:#{first_value}"
puts "sort=#{params.fetch('sort', 'none')}"
puts "tag_count=#{tag_count}"
  1. raw_query ← page=2&sort=name, pairs ← [["page", "2"], ["sort", "name"]]

    1require "uri"23raw_query→ page=2&sort=name = "page=2&sort=name"  #@raw_query="page=1&sort=date", "tag=ruby&tag=trace"4pairs→ [["page", "2"], ["sort", "name"]] = URI.decode_www_form(raw_query)[["page", "2"], ["sort", "name"]]5params→ {"page"=>"2", "sort"=>"name"} = pairs.to_h{"page"=>"2", "sort"=>"name"}67first_key→ page = pairs.first[0]page8first_value→ 2 = pairs.first[1]29tag_count→ 0 = pairs[["page", "2"], ["sort", "name"]].count { |key, _value| key == "tag" }1011puts "raw_query=#{raw_querypage=2&sort=name}"12puts "first=#{first_keypage}:#{first_value2}"13puts "sort=#{params.fetch('sort', 'none')name}"14puts "tag_count=#{tag_count0}"
    outputraw_query=page=2&sort=name
    first=page:2
    sort=name
    tag_count=0
  1. raw_query ← page=1&sort=date, pairs ← [["page", "1"], ["sort", "date"]]

    1require "uri"23raw_query→ page=1&sort=date = "page=1&sort=date"4pairs→ [["page", "1"], ["sort", "date"]] = URI.decode_www_form(raw_query)[["page", "1"], ["sort", "date"]]5params→ {"page"=>"1", "sort"=>"date"} = pairs.to_h{"page"=>"1", "sort"=>"date"}67first_key→ page = pairs.first[0]page8first_value→ 1 = pairs.first[1]19tag_count→ 0 = pairs[["page", "1"], ["sort", "date"]].count { |key, _value| key == "tag" }1011puts "raw_query=#{raw_querypage=1&sort=date}"12puts "first=#{first_keypage}:#{first_value1}"13puts "sort=#{params.fetch('sort', 'none')date}"14puts "tag_count=#{tag_count0}"
    outputraw_query=page=1&sort=date
    first=page:1
    sort=date
    tag_count=0
  1. raw_query ← tag=ruby&tag=trace, pairs ← [["tag", "ruby"], ["tag", "trace"]]

    1require "uri"23raw_query→ tag=ruby&tag=trace = "tag=ruby&tag=trace"4pairs→ [["tag", "ruby"], ["tag", "trace"]] = URI.decode_www_form(raw_query)[["tag", "ruby"], ["tag", "trace"]]5params→ {"tag"=>"trace"} = pairs.to_h{"tag"=>"trace"}67first_key→ tag = pairs.first[0]tag8first_value→ ruby = pairs.first[1]ruby9tag_count→ 2 = pairs[["tag", "ruby"], ["tag", "trace"]].count { |key, _value| key == "tag" }1011puts "raw_query=#{raw_querytag=ruby&tag=trace}"12puts "first=#{first_keytag}:#{first_valueruby}"13puts "sort=#{params.fetch('sort', 'none')none}"14puts "tag_count=#{tag_count2}"
    outputraw_query=tag=ruby&tag=trace
    first=tag:ruby
    sort=none
    tag_count=2