Is there a way to scope variables to the thread without having to pass everything around, given a class with the following methods:
def initialize
@server = TCPServer.new('localhost',456)
end
def start
threads = []
while (upload = @server.accept)
threads << Thread.new(upload) do |connection|
some_method_here(connection)
end
end
threads.each {|t| t.join }
end
def some_method_here(connection)
variable = "abc"
another_method(connection,variable)
end
def another_method(connection,variable)
puts variable.inspect
connection.close
end
if I get you right you want to use thread local variables (see the ruby rdoc for Thread#[])
From the rdoc:
So your example would use
wherever you were using just
variablebefore