# Static variable; behaviour in ruby?

**URL:** <https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324>\
**Category:** Ruby\
**Created:** [November 16, 2005, 2:48pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324 "2005-11-16T14:48:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 16, 2005, 2:48pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324/1 "2005-11-16T14:48:53Z")

</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
```

---

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 16, 2005, 2:54pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324/2 "2005-11-16T14:54:54Z")

</div>

On 16/11/05, Hugh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> state against which x,y,z are compared. The code will be called several  
> 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.

Brian

---

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 16, 2005, 3:07pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324/3 "2005-11-16T15:07:49Z")

</div>

Hugh S. 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:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 16, 2005, 3:44pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324/4 "2005-11-16T15:44:17Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 16, 2005, 3:59pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324/5 "2005-11-16T15:59:21Z")

</div>

On Wed, 16 Nov 2005, Robert K. wrote:

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

> > 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
> 
> ```
> robert
> 
> ```

```
    Thank you,
    Hugh
```

---

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 16, 2005, 4:38pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324/6 "2005-11-16T16:38:33Z")

</div>

On Thu, 17 Nov 2005, Hugh S. wrote:

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

class Line  
def initialize x, y, z, &statements  
@state = rand  
@x, @y, @z = x, y, z  
@statements = statements  
end  
def my\_method  
@statements[@x, @y, @z] if @state \< 0.42  
end  
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

---

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 16, 2005, 3:07pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324/7 "2005-11-16T15:07:49Z")

</div>

On Wed, 16 Nov 2005, Hugh S. wrote:

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

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

a = do\_this\_factory ‘x’,‘y’,‘z’ do |x,y,z| p [x,y,z] end  
a.call

harp:~ \> ruby a.rb

harp:~ \> ruby a.rb

harp:~ \> ruby a.rb  
[“x”, “y”, “z”]

harp:~ \> ruby a.rb

harp:~ \> ruby a.rb  
[“x”, “y”, “z”]

harp:~ \> ruby a.rb  
[“x”, “y”, “z”]

> Maybe there’s another way to do this?

write it in assembler 😉

-a

---

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 16, 2005, 4:05pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324/8 "2005-11-16T16:05:24Z")

</div>

Hugh S. wrote:

> > > the scoping rules, and know where i was.  
> > > variable.  
> > > context…
> > 
> > > calls to this method within the scope, each with its own separate  
> > > 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
```

---

<div class="post-metadata">

**Author:** ![hgs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e5b9ba/32.png) [@hgs](https://www.ruby-forum.com/u/hgs)\
**Post date:** [November 16, 2005, 4:14pm UTC](https://www.ruby-forum.com/t/static-variable-behaviour-in-ruby/48324/9 "2005-11-16T16:14:27Z")

</div>

On Wed, 16 Nov 2005, Ara.T.Howard wrote:

> On Wed, 16 Nov 2005, Hugh S. wrote:
> 
> > Any idea how to create or simulate a static variable in ruby?  
> > […]
> 
> harp:~ \> ruby a.rb  
> [“x”, “y”, “z”]  
> [etc]

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

🙂

> 

```
    Hugh
```
