title: Why does capturing named groups in Ruby result in "undefined local variable or method" errors?
body: <p>I am having trouble with named captures in regular expressions in Ruby 2.0. I have a string variable and an interpolated regular expression:</p>
  
  <pre><code>str = "hello world"
  re = /\w+/
  /(?&lt;greeting&gt;#{re})/ =~ str
  greeting
  </code></pre>
  
  <p>It raises the following exception:</p>
  
  <blockquote>
    <p>prova.rb:4:in <code>&lt;main&gt;': undefined local variable or method</code>greeting' for main:Object (NameError)<br>
    shell returned 1</p>
  </blockquote>
  
  <p>However, the interpolated expression works without named captures. For example:</p>
  
  <pre><code>/(#{re})/ =~ str
  $1
  # =&gt; "hello"
  </code></pre>