Skip to content

Instantly share code, notes, and snippets.

@chrisroos
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisroos/f3010ae22ceb368f52e7 to your computer and use it in GitHub Desktop.
Save chrisroos/f3010ae22ceb368f52e7 to your computer and use it in GitHub Desktop.
Testing the difference between ActiveSupport::SafeBuffer#gsub! and String#gsub!

Testing ActiveSupport::SafeBuffer#gsub!

Although ActiveSupport::SafeBuffer#gsub! appears to work, its behaviour differs from String#gsub!.

These tests show how ActiveSupport::SafeBuffer#gsub! doesn't update Regexp.last_match.

source 'https://rubygems.org'
gem 'activesupport'
gem 'minitest'
GEM
remote: https://rubygems.org/
specs:
activesupport (4.2.2)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
i18n (0.7.0)
json (1.8.3)
minitest (5.7.0)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
activesupport
minitest
BUNDLED WITH
1.10.4
require 'bundler/setup'
require 'minitest/autorun'
require 'active_support'
class RegexpTest < Minitest::Test
def test_behaviour_of_active_support_safe_buffer_and_gsub_bang
string = ActiveSupport::SafeBuffer.new('Hello world')
string.gsub!('ll') do |match|
match.upcase
end
assert_equal 'HeLLo world', string
assert_equal nil, Regexp.last_match # I'd expect this to contain a MatchData object, as it does for String#gsub!
end
def test_behaviour_of_strings_and_gsub_bang
string = 'Hello world'
string.gsub!('ll') do |match|
match.upcase
end
assert_equal 'HeLLo world', string
assert_equal 1, Regexp.last_match.length
assert_equal 'll', Regexp.last_match[0]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment