# Variable scope in GServer

**URL:** <https://www.ruby-forum.com/t/variable-scope-in-gserver/116111>\
**Category:** Ruby\
**Created:** [October 2, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/variable-scope-in-gserver/116111 "2007-10-02T13:45:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [October 2, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/variable-scope-in-gserver/116111/1 "2007-10-02T13:45:26Z")

</div>

(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. I’m sure that my understanding of scope and GServer  
itself  
is causing my problem, but I just don’t know what to do to fix it. I was  
thinking that I could pass the hash as a parameter to the connect  
function  
but that would quickly become a problem as it would need to be passed to  
several other functions after that and if possible I just don’t want to  
have  
to use that many extra parameters in my functions. Any help would be  
appreciated.

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [October 2, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/variable-scope-in-gserver/116111/2 "2007-10-02T17:25:04Z")

</div>

Hi,

On Tue, 2007-10-02 at 20:44 +0900, Chris B. wrote:

> 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.

This is just a random thought, but what about a class-level hash where  
the running thread (Thread.current) is the key?

e.g.

class TestServer#…  
@@thingy = {}  
…  
protected  
def test\_hash  
@@thingy[Thread.current]  
end  
def test\_hash= m  
@@thingy[Thread.current] = m  
end  
end

I’m not sure about locking (which is probably only relevant at the level  
of test\_hash=), but it may be worth a look

Just a thought.

- Arlen

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [October 2, 2007, 7:16pm UTC](https://www.ruby-forum.com/t/variable-scope-in-gserver/116111/3 "2007-10-02T19:16:27Z")

</div>

On Oct 2, 2007, at 6:44 AM, Chris B. wrote:

> ```
> connect(io) #Defined in connect.rb
> 
> ```
> 
> 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 G. II
