From: James Edward Gray II Date: 2007-10-03T02:15:31+09:00 Subject: Re: Variable scope in GServer On Oct 2, 2007, at 6:44 AM, Chris Bailey wrote: > (Repost) > Thank you for the correction Bertram, I've never used a mailing > list before and it didn't occur to me. Hopefully it's in the right > place now. > > I'm having a bit of a problem accessing variables in an instance of > GServer. > What I would like to do is make a hash that is effectively global > to the > current thread. > > -/Server.rb > require 'gserver' > require 'connect.rb' > > class TestServer < GServer > def initialize(port = 4000, *args) > super(port, *args) > end > def serve( io ) > @test_hash = Hash.new > connect(io) #Defined in connect.rb > loop do > str = io.gets > parser(str,io) > end > end > end > > The way that I'm doing it doesn't allow each thread to have it's > own copy of > @test_hash and that's my problem. I need each thread to be able to > change > the data stored in the hash without affecting the data stored in > the hash > for all threads. You could make test_hash a local variable and pass it as a parameter into the methods you need it in. You could also build a new object to represent client state, instead of just using the server state object for everything. When a new client connects, build the client object (passing in your Hash and IO objects) and call some kind of run() method on the client to handle everything else. I hope that helps. James Edward Gray II