From: Robert Klemme Date: 2005-11-17T00:02:57+09:00 Subject: Re: static variable; behaviour in ruby? Hugh Sasse wrote: > On Wed, 16 Nov 2005, Robert Klemme wrote: > >> 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 > > Because I can't see how to call it into being correctly. If I need > to know "when this method was called the first time" to do this, > then I need something with the same properties as a static variable, > in order to implement a thing with the same properties as a static > variable. > >> scopes by the nuber of created instances. > > But I need to know when to create them for the string I'm > evaluating, rather than when to call on existing ones in that string. >> >> Also, IMHO Pit has a valid point with his remark about a >> contradiction. > > In ruby you can have several flip-flop expressions in the same > context... >> >>> 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)? > > There is no concurrency in the thing at the moment, so I'm not sure > how to make use of that. Maybe it is a design pattern I've missed. > > I'm beginning to think it is nontrivial, and will probably to to use > gawk to avoid sed in this case. I'm not yet convinced that it's actually so complicated. Please see my other posting. robert