Fix for SafeBuffer#gsub and magic match globs #1622

Closed
wants to merge 4 commits into
from
@@ -116,6 +116,22 @@ def #{unsafe_method}!(*args)
end
EOT
end
+
+ # Provides a special-case override for String#gsub
+ # to preserve some of the standard behaviour with respect
+ # to magic matching global variables
+ #
+ # Unlike standard gsub, magic matching globs are _not_
+ # available in code following a SafeBuffer#gsub call,
+ # but they are available within a block passed to gsub, eg:
+ # SafeBuffer.new('m').gsub(/(m)/){ $1 }
+ # In this case $1 == 'm' within the block, but not afterwards
+ #
+ # If you really need the magic matching variables after the gsub call
+ # you will need to convert SafeBuffer to a String first
+ def gsub(*args, &block)
+ to_str.gsub(*args, &block)
+ end
end
end
@@ -50,4 +50,24 @@ def setup
@buffer.gsub!('', 'asdf')
end
end
+
+ test "Should set magic match variables within block passed to gsub" do
+ 'burn'[/(matches)/]
+ @buffer << 'swan'
+ result = nil
+ @buffer.gsub(/(swan)/) { result = $1 }
+ assert_equal 'swan', result, "dang it, still not working"
+ end
+
+ test "Should not expect magic match variables after gsub call" do
+ 'burn'[/(matches)/]
+ @buffer << 'vesta'
+ @buffer.gsub(/(vesta)/, '')
+ assert !$1, %(
+ if you can make this test fail it is a _good_ thing: somehow you have
+ restored the standard behaviour of SafeBuffer#gsub to make magic matching
+ variables available after the call, and you could invert this test
+ )
+ end
+
end