How about using URI method directly:

<pre>
require 'uri'
url = URI.new('...')
uri.path.count('/') ...
</pre>