rails/rails — Issue #3496: Global variables for referencing submatches broken with ActiveSupport::SafeBuffer#gsub + block
Author: DerGuteMoritz
Created: 2011-11-02 17:18:16+00:00
State: open

When calling `gsub` with a block on an `ActiveSupport::SafeBuffer` the global variables `$1`, `$2`, etc. for referencing submatches are not always properly set (anymore?) when the block is called. So far I could only reproduce it when an `ActiveSupport::SafeBuffer` instance is passed to a method which in turn calls `gsub` with a block on it like this:

```ruby
def foo(x)
x.gsub(/(a)/) { |m| $1 + 'b' }
end

foo('a'.html_safe) # NoMethodError: undefined method `+' for nil:NilClass
```

Whereas:

```ruby
foo('a') # => "ab"
'a'.gsub(/(a)/) { |m| $1 + 'b' } # => "ab"
```

This is true for Ruby 1.8.7-p352 and 1.9.2-p290 at least and affects at least Rails 3.0.x and 3.1.x.