Skip to content

Instantly share code, notes, and snippets.

@rennex
Created March 15, 2020 10:48
Show Gist options
  • Save rennex/f18e4468faf680439f94c60785daa503 to your computer and use it in GitHub Desktop.
Save rennex/f18e4468faf680439f94c60785daa503 to your computer and use it in GitHub Desktop.
Special variable $1 getting clobbered from another thread
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