Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Parse a URL and extract all the parts in Ruby Language

Posted on July 23, 2021July 23, 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 below information from the URI

  • Scheme
  • User Info
  • Hostname
  • Port
  • Pathname
  • Query Params
  • Fragment

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

Then

  • Scheme is HTTPS
  • User Info – username is test and password is abcd123. Username and password are separated by a colon :
  • Hostname is www.techbyexample.com
  • Port is 8000
  • The path is tutorials/intro
  • Query Params is type=advance and compact=false. They are separated by ampersand
  • fragment is history. It will directly take to the history section within that page. history is an identifier that refers to that section within that page

Program

require 'uri'

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

pasrse_uri = URI(uri)

puts(pasrse_uri.scheme)  
puts(pasrse_uri.userinfo)  
puts(pasrse_uri.host)
puts(pasrse_uri.port)
puts(pasrse_uri.path)
puts(pasrse_uri.query)
puts(pasrse_uri.fragment)
puts(pasrse_uri.to_s)

Output

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

It correctly dumps all the information as seen from the output

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