convert string to hash ruby

string = "key1=value1&key2=value2&key3=value3"
hash = {}

string.split("&").each do |pair|
  key, value = pair.split("=")
  hash[key] = value
end

puts hash.inspect

Explanation: 1. Initialize a string variable string with the input string. 2. Create an empty hash named hash. 3. Split the string using & as a delimiter, creating an array of key-value pairs. 4. Iterate through each pair using each. 5. Within the loop, split each pair using = as a delimiter, separating keys and values. 6. Assign the key and value to variables key and value respectively. 7. Add each key-value pair to the hash. 8. Output the resulting hash using puts and inspect to display its contents.