title: When using the $1-$9 regular expression form, why does calling gsub(...) on one nil the others?
body: <p>First, a working example:</p>
  
  <pre><code>string = "foo-bar-25-baz"
  if string =~ /(.+)-(10|25)(?:-(baz))?/
    puts $1 
    puts $2
    puts $3
  end
  </code></pre>
  
  <p>This produces 'foo-bar', '25' and 'baz' on three lines, as expected. But if we do this:</p>
  
  <pre><code>string = "foo-bar-25-baz"
  if string =~ /(.+)-(10|25)(?:-(baz))?/
    puts $1.gsub('-', ' ') # Here be the problem
    puts $2 # nil
    puts $3 # nil
  end
  </code></pre>
  
  <p>the values of <code>$2</code> and <code>$3</code> are now nil. I have to <code>puts $2</code> and <code>puts $3</code> and <em>then</em> <code>$1.gsub(...)</code>, and it will work. As far as I can tell this only applies to <code>gsub</code> and <code>gsub!</code></p>
  
  <p>This causes the same problem:</p>
  
  <pre><code>string = "foo-bar-25-baz"
  if string =~ /(.+)-(10|25)(?:-(baz))?/
    puts $3.gsub('hard', 'h')
    puts $1 # nil
    puts $2 # nil
  end
  </code></pre>
  
  <p>I spent about 15 minutes debugging this and I'm wondering why.</p>