tl;dr
Don't forget
require 'English'
if you use a named global such as$LAST_MATCH_INFO
. Otherwise this could result in an annoying bug.
The code snippet of How to fix gsub on SafeBuffer objects was adjusted during a rubocop upgrade.
Rubocop tells you everything you need to know to fix the offense Style/SpecialGlobalVars
:
Style/SpecialGlobalVars: Prefer $LAST_MATCH_INFO from the stdlib 'English' module (don't forget to require it) over $~.
In this scenario you should be very careful. If you forget to require 'English'
, your code may not work any longer as expected.
# Before
Thread.current[:LAST_MATCH_DATA] = $~
# After rubocop ugrade - was not working as expected
Thread.current[:LAST_MATCH_DATA] = $LAST_MATCH_INFO
# Fixed
require 'English'
Thread.current[:LAST_MATCH_DATA] = $LAST_MATCH_INFO