From: Robert Klemme Date: 2005-11-16T23:42:17+09:00 Subject: Re: static variable; behaviour in ruby? Hugh Sasse wrote: > Any idea how to create or simulate a static variable in ruby? I completely agree to Brian's comment. > I want to create a variable to hold state between calls of a method, > but have the state disappear when the method goes out of scope. > I could create it in some external object but then I'd need to query > the scoping rules, and know where i was. I'm not sure I understand you correctly here. Why don't you just create an instance that holds this state plus whatever is needed and implement your behavior in a method of that class? You can control the number of scopes by the nuber of created instances. Also, IMHO Pit has a valid point with his remark about a contradiction. > I'm trying to do an instance_eval on a string containing a method > such as do_this x,y,z {block} > and I want to evaluate the block, or not, as a function of some > state against which x,y,z are compared. The code will be called > several times, but I only want the state intialized the first time > the scope > is entered. > > And, just to make matters worse, there could be several independent > calls to this method within the scope, each with its own separate > state. > > Maybe there's another way to do this? Did you consider using a Thread local stack for this (as I conclude from your latest posting nesting is possible)? Kind regards robert