Skip to content

Instantly share code, notes, and snippets.

@mfn
Created May 21, 2011 20:18
Show Gist options
  • Save mfn/984864 to your computer and use it in GitHub Desktop.
Save mfn/984864 to your computer and use it in GitHub Desktop.
Have an uri give me a hash with all it's components
require 'uri'
require 'pp'
uri = URI.parse "http://foo/bar?baz&frob#blamp"
pp uri.component.each_with_object({}).find_all { |c,h| h[c] = uri.send(c) }
@mfn
Copy link
Author

mfn commented May 21, 2011

Gives:

[[:scheme,
  {:scheme=>"http",
   :userinfo=>nil,
   :host=>"foo",
   :port=>80,
   :path=>"/bar",
   :query=>"baz&frob",
   :fragment=>"blamp"}],
 [:host,
  {:scheme=>"http",
   :userinfo=>nil,
   :host=>"foo",
   :port=>80,
   :path=>"/bar",
   :query=>"baz&frob",
   :fragment=>"blamp"}],
 [:port,
  {:scheme=>"http",
   :userinfo=>nil,
   :host=>"foo",
   :port=>80,
   :path=>"/bar",
   :query=>"baz&frob",
   :fragment=>"blamp"}],
 [:path,
  {:scheme=>"http",
   :userinfo=>nil,
   :host=>"foo",
   :port=>80,
   :path=>"/bar",
   :query=>"baz&frob",
   :fragment=>"blamp"}],
 [:query,
  {:scheme=>"http",
   :userinfo=>nil,
   :host=>"foo",
   :port=>80,
   :path=>"/bar",
   :query=>"baz&frob",
   :fragment=>"blamp"}],
 [:fragment,
  {:scheme=>"http",
   :userinfo=>nil,
   :host=>"foo",
   :port=>80,
   :path=>"/bar",
   :query=>"baz&frob",
   :fragment=>"blamp"}]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment