Extract url params in ruby
<p>I would like to extract parameters from url. I have following path pattern:</p>

<pre><code>pattern = "/foo/:foo_id/bar/:bar_id"
</code></pre>

<p>And example url:</p>

<pre><code>url = "/foo/1/bar/2"
</code></pre>

<p>I would like to get {foo_id: 1, bar_id: 2}. I tried to convert pattern into something like this:</p>

<pre><code>"\/foo\/(?&lt;foo_id&gt;.*)\/bar\/(?&lt;bar_id&gt;.*)"
</code></pre>

<p>I failed on first step when I wanted to replace backslash in url:</p>

<pre><code>formatted = pattern.gsub("/", "\/")
</code></pre>

<p>Do you know how to fix this gsub? Maybe you know better solution to do this.</p>

<p>EDIT:
It is plain Ruby. I am not using RoR.</p>