title: Ruby/Rails 3.1: Given a URL string, remove path
body: <p>Given any valid HTTP/HTTPS string, I would like to parse/transform it such that the end result is exactly the root of the string. </p>
  
  <p>So given URLs:</p>
  
  <pre><code>http://foo.example.com:8080/whatsit/foo.bar?x=y
  https://example.net/
  </code></pre>
  
  <p>I would like the results:</p>
  
  <pre><code>http://foo.example.com:8080/
  https://example.net/
  </code></pre>
  
  <p>I found the <a href="http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Parser.html" rel="noreferrer">documentation</a> for URI::Parser not super approachable. </p>
  
  <p>My initial, naïve solution would be a simple regex like:</p>
  
  <pre><code>/\A(https?:\/\/[^\/]+\/)/
  </code></pre>
  
  <p>(That is: Match up to the first slash after the protocol.)</p>
  
  <p>Thoughts &amp; solutions welcome. And apologies if this is a duplicate, but my search results weren't relevant.</p>