require 'uri'
# Function to parse and decode a complete URL query string into a Hash
def parse_and_decode_query(query_string)
# Remove leading '?' if present
query_string = query_string.sub(/^\?/, '')
# URI.decode_www_form returns an array of [key, value] pairs,
# which can be converted directly into a Ruby Hash.
# Note: to_h uses the last value if duplicate keys exist.
URI.decode_www_form(query_string).to_h
rescue ArgumentError => e
puts "Parsing error: #{e.message}"
{}
end
# --- Runnable Program Execution ---
if __FILE__ == $0
full_query = "search=ruby+tutorial&category=programming&tag=beginner"
puts "--- Full Query String Parsing ---"
puts "Original Query: #{full_query}"
result_hash = parse_and_decode_query(full_query)
puts "Decoded Hash Result:"
result_hash.each do |key, value|
puts " #{key} => #{value}"
end
end
# run:
#
# --- Full Query String Parsing ---
# Original Query: search=ruby+tutorial&category=programming&tag=beginner
# Decoded Hash Result:
# search => ruby tutorial
# category => programming
# tag => beginner
#