Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Convert Query Param String to Query Param Hash in Ruby Language

Posted on July 23, 2021July 23, 2021 by admin

Overview

Assume we have below query param string

a=b&x=y

We want the output as

{
 "a" => "b",
 "x" => "y"
}

Program

Below is the program for the same

input_sring = 'a=b&x=y'

query_params_hash = {}
input_string_split = input_sring.split('&')
input_string_split.each do |q|
    q_split = q.split('=')
    query_params_hash[q_split[0]] = q_split[1]
end 

puts query_params_hash

Output

{"a"=>"b", "x"=>"y"}
  • ruby
  • ©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme