# Static variable; behaviour in ruby?

**URL:** <https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522>\
**Category:** ruby-talk\
**Created:** [16 November 2005 13:47 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522 "2005-11-16T13:47:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)\
**Post date:** [16 November 2005 13:47 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/1 "2005-11-16T13:47:14Z")

</div>

Any idea how to create or simulate a static variable in ruby?

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 trying to do an instance\_eval on a string containing a method such as  
&nbsp;&nbsp;&nbsp;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?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thank you  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh

---

<div class="post-metadata">

**Author:** ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)\
**Post date:** [16 November 2005 13:52 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/2 "2005-11-16T13:52:28Z")

</div>

I advise that you state your basic problem, as this seems to be a  
problem with an ugly workaround. You can probably restructure your  
problem such that this is no longer needed.

Brian

> **···**
>
> On 16/11/05, Hugh Sasse \<hgs@dmu.ac.uk\> wrote:
> 
> > Any idea how to create or simulate a static variable in ruby?
> > 
> > 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 trying to do an instance\_eval on a string containing a method such as  
> > &nbsp;&nbsp;&nbsp;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?
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thank you  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh
> 
> --  
> [http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)
> 
> Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [16 November 2005 14:05 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/3 "2005-11-16T14:05:40Z")

</div>

Hugh Sasse schrieb:

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

and

> And, just to make matters worse, there could be several independent  
> calls to this method within the scope, each with its own separate  
> state.

Isn't this a contradiction?

When should the state appear and when should it disappear? What should be the scope of the state?

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [16 November 2005 14:07 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/4 "2005-11-16T14:07:28Z")

</div>

> Any idea how to create or simulate a static variable in ruby?
> 
> 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 trying to do an instance\_eval on a string containing a method such as  
> &nbsp;&nbsp;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.

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;def do\_this\_factory x, y, z, &block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state = rand # do something with x, y, z  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lambda{ block[x, y, z] if state \< 0.42 }  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;a = do\_this\_factory 'x','y','z' do |x,y,z| p [x,y,z] end  
&nbsp;&nbsp;&nbsp;a.call

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;["x", "y", "z"]

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;["x", "y", "z"]

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;["x", "y", "z"]

> Maybe there's another way to do this?

write it in assembler 😉

-a

> **···**
>
> # On Wed, 16 Nov 2005, Hugh Sasse wrote: --
> 
> > ara [dot] t [dot] howard [at] gmail [dot] com  
> > all happiness comes from the desire for others to be happy. all misery  
> > comes from the desire for oneself to be happy.  
> > -- bodhicaryavatara
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [16 November 2005 14:42 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/5 "2005-11-16T14:42:17Z")

</div>

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  
> &nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)\
**Post date:** [16 November 2005 14:30 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/6 "2005-11-16T14:30:24Z")

</div>

> \> Any idea how to create or simulate a static variable in ruby?  
> \>  
> \> 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 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?  
> \>  
> \> Thank you  
> \> Hugh  
> \>  
> \>
> 
> I advise that you state your basic problem, as this seems to be a  
> problem with an ugly workaround. You can probably restructure your  
> problem such that this is no longer needed.

I thought the paragraph about instance\_eval explained it :-).

Essentially, I have become fed up with lousy diagnostics from sed,  
and have been trying to re-implement (enough of) it in Ruby, so I  
hae some chance of getting the code to work. Thus I'd use code like:

&nbsp;&nbsp;&nbsp;within 10, 30 {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/this/, 'that')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/thingy/, 'the other')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Example of munging something akin to C comments  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;within /^\s+\/\*/, /\*\/\s\*$/ {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rot13 # or something  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;}

So I need to keep track of which patterns have matched, and clear  
them when the scope (surrounding block) is gone.

> Brian

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh

> **···**
>
> On Wed, 16 Nov 2005, Brian Schröder wrote:
> 
> > On 16/11/05, Hugh Sasse \<hgs@dmu.ac.uk\> wrote:

---

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)\
**Post date:** [16 November 2005 14:56 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/7 "2005-11-16T14:56:19Z")

</div>

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

> Kind regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thank you,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh

> **···**
>
> On Wed, 16 Nov 2005, Robert Klemme wrote:
> 
> >

---

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)\
**Post date:** [16 November 2005 15:11 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/8 "2005-11-16T15:11:45Z")

</div>

> \> Any idea how to create or simulate a static variable in ruby?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]

> &nbsp;&nbsp;harp:~ \> cat a.rb  
> &nbsp;&nbsp;def do\_this\_factory x, y, z, &block  
> &nbsp;&nbsp;&nbsp;&nbsp;state = rand # do something with x, y, z  
> &nbsp;&nbsp;&nbsp;&nbsp;lambda{ block[x, y, z] if state \< 0.42 }  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;a = do\_this\_factory 'x','y','z' do |x,y,z| p [x,y,z] end  
> &nbsp;&nbsp;a.call  
> &nbsp;&nbsp;harp:~ \> ruby a.rb
> 
> &nbsp;&nbsp;harp:~ \> ruby a.rb  
> &nbsp;&nbsp;["x", "y", "z"]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[etc]

Maybe something like  
&nbsp;&nbsp;&nbsp;&nbsp;def my\_method x, y, z, &block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state = rand # do something with x, y, z  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias :old\_my\_method, :my\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:my\_method)(x,y,z, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lambda{ block[x, y, z] if state \< 0.42 }.call  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

might do the trick, then? The state would get carried around with  
the closure in subesequent calls to the same method? Except having  
sevaral calls in the same block would be treated the same as severl  
calls to the same method, rather than the way sed treats  
/this/,/that/{ statements}, each heing independent, runnin for each  
line of the input.

> \> Maybe there's another way to do this?
> 
> write it in assembler 😉

🙂

> 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh

> **···**
>
> On Wed, 16 Nov 2005, Ara.T.Howard wrote:
> 
> > On Wed, 16 Nov 2005, Hugh Sasse wrote:

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [16 November 2005 15:02 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/9 "2005-11-16T15:02:57Z")

</div>

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  
> > > > &nbsp;&nbsp;&nbsp;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.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [16 November 2005 15:03 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/10 "2005-11-16T15:03:08Z")

</div>

Hugh Sasse wrote:

> > > Any idea how to create or simulate a static variable in ruby?
> > > 
> > > 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 trying to do an instance\_eval on a string containing a method  
> > > &nbsp;&nbsp;&nbsp;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?
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thank you  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh
> > 
> > I advise that you state your basic problem, as this seems to be a  
> > problem with an ugly workaround. You can probably restructure your  
> > problem such that this is no longer needed.
> 
> I thought the paragraph about instance\_eval explained it :-).
> 
> Essentially, I have become fed up with lousy diagnostics from sed,  
> and have been trying to re-implement (enough of) it in Ruby, so I  
> hae some chance of getting the code to work. Thus I'd use code like:
> 
> &nbsp;&nbsp;&nbsp;within 10, 30 {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/this/, 'that')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/thingy/, 'the other')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Example of munging something akin to C comments  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;within /^\s+\/\*/, /\*\/\s\*$/ {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rot13 # or something  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;}
> 
> So I need to keep track of which patterns have matched, and clear  
> them when the scope (surrounding block) is gone.

Let's see whether I understand this correctly: you want your method  
"within" to create something that can be later thrown against the current  
line of the input and execute the given block only if the condition  
matches? Wouldn't it be easier to just call within for each line of the  
input?

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > On Wed, 16 Nov 2005, Brian Schröder wrote:
> > 
> > > On 16/11/05, Hugh Sasse \<hgs@dmu.ac.uk\> wrote:

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [16 November 2005 15:35 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/11 "2005-11-16T15:35:44Z")

</div>

> Maybe something like  
> &nbsp;&nbsp;&nbsp;def my\_method x, y, z, &block  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state = rand # do something with x, y, z  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias :old\_my\_method, :my\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:my\_method)(x,y,z, &block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lambda{ block[x, y, z] if state \< 0.42 }.call  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end

but the second time my\_method is called you'll clobber old\_my\_method - is that  
what you want? plus it only works once because the first call to my\_method  
will define it in such a way that it never sets of state again - it's  
clobbered by the define\_method/lambda bit...

> might do the trick, then? The state would get carried around with the  
> closure in subesequent calls to the same method? Except having sevaral  
> calls in the same block would be treated the same as severl calls to the  
> same method, rather than the way sed treats /this/,/that/{ statements}, each  
> heing independent, runnin for each line of the input.

i really don't quite get what you are after. but from what i'm reading it  
looks alot like you would be better off doing something like this

&nbsp;&nbsp;&nbsp;class Line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize x, y, z, &statements  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@state = rand  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@x, @y, @z = x, y, z  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@statements = statements  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def my\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@statements[@x, @y, @z] if @state \< 0.42  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

put another way - a static variable can be emulated as an instance variable of  
an object. you can have multiple copies by having multiple instances. the  
initialize methods makes sure it's only called once.

this might be easier?

-a

> **···**
>
> # On Thu, 17 Nov 2005, Hugh Sasse wrote: --
> 
> > ara [dot] t [dot] howard [at] gmail [dot] com  
> > all happiness comes from the desire for others to be happy. all misery  
> > comes from the desire for oneself to be happy.  
> > -- bodhicaryavatara
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)\
**Post date:** [16 November 2005 15:19 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/12 "2005-11-16T15:19:09Z")

</div>

> Hugh Sasse wrote:  
> \>  
> \>\>\> Any idea how to create or simulate a static variable in ruby?  
> \>\>\>  
> \>\>\> 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 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?  
> \>\>\>  
> \>\>\> Thank you  
> \>\>\> Hugh  
> \>\>\>  
> \>\>\>  
> \>\>  
> \>\> I advise that you state your basic problem, as this seems to be a  
> \>\> problem with an ugly workaround. You can probably restructure your  
> \>\> problem such that this is no longer needed.  
> \>  
> \> I thought the paragraph about instance\_eval explained it :-).  
> \>  
> \> Essentially, I have become fed up with lousy diagnostics from sed,  
> \> and have been trying to re-implement (enough of) it in Ruby, so I  
> \> hae some chance of getting the code to work. Thus I'd use code like:  
> \>  
> \> within 10, 30 {  
> \> sub(/this/, 'that')  
> \> sub(/thingy/, 'the other')  
> \> # Example of munging something akin to C comments  
> \> within /^\s+\/\*/, /\*\/\s\*$/ {  
> \> rot13 # or something  
> \> }  
> \> }  
> \>  
> \> So I need to keep track of which patterns have matched, and clear  
> \> them when the scope (surrounding block) is gone.
> 
> Let's see whether I understand this correctly: you want your method  
> "within" to create something that can be later thrown against the current  
> line of the input and execute the given block only if the condition  
> matches? Wouldn't it be easier to just call within for each line of the  
> input?

all the statements in the input language which I'm processing with  
instance\_eval (for now) would be called for each line of the input.  
So when each one that must hold state is called for the first time  
it must clear the state, for future calls to that function on that  
source line, but later input lines, it must not "cold start" the  
state.

For within(), I'm trying to model sed's '/this/, /that/ { do\_this}'  
And I could have several withins in the same chunk of input  
language.

> Kind regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thank you,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh

> **···**
>
> On Thu, 17 Nov 2005, Robert Klemme wrote:
> 
> > \> On Wed, 16 Nov 2005, Brian Schröder wrote:  
> > \>\> On 16/11/05, Hugh Sasse \<hgs@dmu.ac.uk\> wrote:

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [16 November 2005 16:07 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/13 "2005-11-16T16:07:17Z")

</div>

Hugh Sasse wrote:

> > Hugh Sasse wrote:
> > 
> > > > > Any idea how to create or simulate a static variable in ruby?
> > > > > 
> > > > > 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 trying to do an instance\_eval on a string containing a method  
> > > > > &nbsp;&nbsp;&nbsp;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?
> > > > > 
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thank you  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh
> > > > 
> > > > I advise that you state your basic problem, as this seems to be a  
> > > > problem with an ugly workaround. You can probably restructure your  
> > > > problem such that this is no longer needed.
> > > 
> > > I thought the paragraph about instance\_eval explained it :-).
> > > 
> > > Essentially, I have become fed up with lousy diagnostics from sed,  
> > > and have been trying to re-implement (enough of) it in Ruby, so I  
> > > hae some chance of getting the code to work. Thus I'd use code like:
> > > 
> > > &nbsp;&nbsp;&nbsp;within 10, 30 {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/this/, 'that')  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub(/thingy/, 'the other')  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Example of munging something akin to C comments  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;within /^\s+\/\*/, /\*\/\s\*$/ {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rot13 # or something  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > So I need to keep track of which patterns have matched, and clear  
> > > them when the scope (surrounding block) is gone.
> > 
> > Let's see whether I understand this correctly: you want your method  
> > "within" to create something that can be later thrown against the  
> > current line of the input and execute the given block only if the  
> > condition matches? Wouldn't it be easier to just call within for  
> > each line of the input?
> 
> all the statements in the input language which I'm processing with  
> instance\_eval (for now) would be called for each line of the input.  
> So when each one that must hold state is called for the first time  
> it must clear the state, for future calls to that function on that  
> source line, but later input lines, it must not "cold start" the  
> state.
> 
> For within(), I'm trying to model sed's '/this/, /that/ { do\_this}'  
> And I could have several withins in the same chunk of input  
> language.

I think I start getting a better understanding of your issue. How about:

def within(\*a, &b)  
&nbsp;&nbsp;my\_state = (Thread.current[:within] || = {})[a] ||= WithinState.new(a,b)  
&nbsp;&nbsp;my\_state.feed(current\_line)  
end

If you want to make this safe against multiple invocations with similar  
parameters you might use another identifying thing (possibly invoking  
caller to determine source code location).

Instead of the thread local you could also use a global but IMHO this is  
cleaner.

HTH

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > On Thu, 17 Nov 2005, Robert Klemme wrote:
> > 
> > > > On Wed, 16 Nov 2005, Brian Schröder wrote:
> > > > 
> > > > > On 16/11/05, Hugh Sasse \<hgs@dmu.ac.uk\> wrote:

---

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://rubytalk.org/u/Hugh_Sasse)\
**Post date:** [16 November 2005 16:27 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/14 "2005-11-16T16:27:43Z")

</div>

> Hugh Sasse wrote:  
> \>  
> \>\> Let's see whether I understand this correctly: you want your method  
> \>\> "within" to create something that can be later thrown against the  
> \>\> current line of the input and execute the given block only if the  
> \>\> condition matches? Wouldn't it be easier to just call within for  
> \>\> each line of the input?  
> \>  
> \> all the statements in the input language which I'm processing with  
> \> instance\_eval (for now) would be called for each line of the input.  
> \> So when each one that must hold state is called for the first time  
> \> it must clear the state, for future calls to that function on that  
> \> source line, but later input lines, it must not "cold start" the  
> \> state.

My use of "input" for supplied source code \*and\* data to process was  
less than helpful. Sorry about that.

> \>  
> \> For within(), I'm trying to model sed's '/this/, /that/ { do\_this}'  
> \> And I could have several withins in the same chunk of input  
> \> language.
> 
> I think I start getting a better understanding of your issue. How about:
> 
> def within(\*a, &b)  
> &nbsp;&nbsp;my\_state = (Thread.current[:within] || = {})[a] ||= WithinState.new(a,b)  
> &nbsp;&nbsp;my\_state.feed(current\_line)  
> end
> 
> If you want to make this safe against multiple invocations with similar  
> parameters you might use another identifying thing (possibly invoking  
> caller to determine source code location).

Yes, I was looking for that: I'd forgotten I could use caller in  
something eval'd.

Making it thread local means it outlives the scope of def and is not  
in the normal global namespace. That's a neat idea.  
I think this will do everything I want....

> Instead of the thread local you could also use a global but IMHO this is  
> cleaner.
> 
> HTH
> 
> Kind regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thank you,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh

> **···**
>
> On Thu, 17 Nov 2005, Robert Klemme wrote:
> 
> > \> On Thu, 17 Nov 2005, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [16 November 2005 16:47 UTC](https://rubytalk.org/t/static-variable-behaviour-in-ruby/22522/15 "2005-11-16T16:47:17Z")

</div>

Hugh Sasse wrote:

> > Hugh Sasse wrote:
> > 
> > > > Let's see whether I understand this correctly: you want your method  
> > > > "within" to create something that can be later thrown against the  
> > > > current line of the input and execute the given block only if the  
> > > > condition matches? Wouldn't it be easier to just call within for  
> > > > each line of the input?
> > > 
> > > all the statements in the input language which I'm processing with  
> > > instance\_eval (for now) would be called for each line of the input.  
> > > So when each one that must hold state is called for the first time  
> > > it must clear the state, for future calls to that function on that  
> > > source line, but later input lines, it must not "cold start" the  
> > > state.
> 
> My use of "input" for supplied source code \*and\* data to process was  
> less than helpful. Sorry about that.
> 
> > > For within(), I'm trying to model sed's '/this/, /that/ { do\_this}'  
> > > And I could have several withins in the same chunk of input  
> > > language.
> > 
> > I think I start getting a better understanding of your issue. How  
> > about:
> > 
> > def within(\*a, &b)  
> > &nbsp;&nbsp;my\_state = (Thread.current[:within] || = {})[a] ||=  
> > &nbsp;&nbsp;WithinState.new(a,b) my\_state.feed(current\_line)  
> > end
> > 
> > If you want to make this safe against multiple invocations with  
> > similar parameters you might use another identifying thing (possibly  
> > invoking caller to determine source code location).
> 
> Yes, I was looking for that: I'd forgotten I could use caller in  
> something eval'd.
> 
> Making it thread local means it outlives the scope of def and is not  
> in the normal global namespace. That's a neat idea.

And it's thread safe and automatically GC'ed on thread termination. 🙂

> I think this will do everything I want....

Cool! Glad I could help.

Cheers

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > On Thu, 17 Nov 2005, Robert Klemme wrote:
> > 
> > > > On Thu, 17 Nov 2005, Robert Klemme wrote:
