Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Get or Extract Query Params from a URL in Ruby Language

Posted on July 28, 2021July 28, 2021 by admin

Overview

URI module can be used to parse a URL and extract all parts

https://ruby-doc.org/stdlib-2.5.1/libdoc/uri/rdoc/URI.html

Once the given URL is parsed correctly, then it will return the URI object. We can then access the query_parms from that URL object

Let’s see a working program for the same:

We will parse the below URL

https://test:abcd123@techbyexample.com:8000/tutorials/intro?type=advance&compact=false#history

Query Params is type=advance and compact=false in the above URL. They are separated by an ampersand

Program

require 'uri'

uri = "https://test:abcd123@techbyexample.com:8000/tutorials/intro?type=advance&compact=false#history"
pasrse_uri = URI(uri)

puts(pasrse_uri.query)

Output

type=advance&compact=false

It correctly dumps all the query params as seen from the output

©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme