Project

General

Profile

Bug #16448

regex global variables are working differently depending on scope

Added by tdrive (Stanislav Boldaev) 8 months ago. Updated 8 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin18]
[ruby-core:96429]
Tags:

Description

Hi, everyone.

I am trying to write a code similar to this

#!/usr/bin/env ruby

def check(f1)
  f2 = ->(match) { p match; p $~ }

  data = "hello test test test"

  data.gsub(/test/, &f1)
  data.gsub(/test/, &f2)
end

check(->(match) { p match; p $~ })

output:

"test"
nil
"test"
nil
"test"
nil
"test"
#<MatchData "test">
"test"
#<MatchData "test">
"test"
#<MatchData "test">

ruby versions:
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin18]
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin18]

Why does the global variable ($~) in f1 and f2 work differently? It's a bug or my mistake?


Related issues

Related to Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localOpenko1 (Koichi Sasada)Actions

Also available in: Atom PDF