Created
March 15, 2020 10:48
-
-
Save rennex/f18e4468faf680439f94c60785daa503 to your computer and use it in GitHub Desktop.
Special variable $1 getting clobbered from another thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProcCaller | |
def add(&block) | |
(@procs ||= []) << block | |
end | |
def callem! | |
@procs.map do |p| | |
Thread.new { p.call } | |
end.each &:join | |
end | |
end | |
pc = ProcCaller.new | |
pc.add do | |
"foobar" =~ /(.)ar/ | |
puts "$1 is #{$1.inspect}" | |
sleep 0.4 | |
puts "$1 is now #{$1.inspect}" | |
end | |
pc.add do | |
sleep 0.2 | |
"x" =~ /(.)/ | |
puts "boop!" | |
end | |
pc.callem! | |
# $1 is "b" | |
# boop! | |
# $1 is now "x" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment