# C's static equivalent

**URL:** <https://rubytalk.org/t/cs-static-equivalent/7277>\
**Category:** ruby-talk\
**Created:** [7 August 2003 14:18 UTC](https://rubytalk.org/t/cs-static-equivalent/7277 "2003-08-07T14:18:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Djd15](https://avatars.discourse-cdn.com/v4/letter/d/0ea827/32.png) [@Djd15](https://rubytalk.org/u/Djd15)\
**Post date:** [7 August 2003 14:18 UTC](https://rubytalk.org/t/cs-static-equivalent/7277/1 "2003-08-07T14:18:17Z")

</div>

More or less:

def make\_closure  
perm\_variable = 0  
return proc {  
perm\_variable += 1  
puts "I have been called “, perm\_variable, " times.”  
}  
end

def use\_closure  
fun = make\_closure  
fun.call  
fun.call  
fun.call  
end

use\_closure

Should print out

I have been called 1 times.  
I have been called 2 times.  
I have been called 3 times.

However, each time you call make\_closure, it returns a separate context, so:

def use\_closure2  
fun1 = make\_closure  
fun2 = make\_closure

fun1.call  
fun2.call  
fun1.call  
end

use\_closure2

Should print:

I have been called 1 times.  
I have been called 1 times.  
I have been called 2 times.

Because fun1 and fun2 store two separate states of perm\_variable.  
At least that’s my understanding of closures.

As far as I know, there’s nothing quite like C’s static  
in Ruby, since there’s no way to make a variable local  
in scope to a function, but persistant in state over all  
calls to that function (at least that I know of).

- Dan

> **···**
>
> ----- Original Message -----  
> From: KONTRA Gergely [kgergely@mlabdial.hit.bme.hu](mailto:kgergely@mlabdial.hit.bme.hu)  
> Date: Thursday, August 7, 2003 9:59 am  
> Subject: Re: C’s static equivalent
> 
> > On 0807, ts wrote:
> 
> > > > Is there something similar to C’s static keyword?  
> > > > I want a single function, which has to access and modify a  
> > > > variable. Any solution without bundling the function  
> > > > to a class?  
> > > > Use a closure  
> > > > Could you be more verbose? Eg. how to print out how many times a  
> > > > function was called.
> > 
> > ## Gergo
> > 
> > ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]------  
> > —+
> > 
> > > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok,  
> > > hogy |  
> > > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene  
> > > jarnom” |  
> > > ±- Magyar php mirror es magyar php dokumentacio:  
> > > [http://hu.php.net](http://hu.php.net) --+

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [7 August 2003 14:38 UTC](https://rubytalk.org/t/cs-static-equivalent/7277/2 "2003-08-07T14:38:31Z")

</div>

A C ‘static’ variable is just a global variable with a lexically local  
scope, so a class variable (@@count += 1) is probably the closest  
equivalent. The scope extends to the whole object though, not just the  
method.

Regards,

Brian.

> **···**
>
> On Thu, Aug 07, 2003 at 11:18:17PM +0900, djd15@cwru.edu wrote:
> 
> > As far as I know, there’s nothing quite like C’s static  
> > in Ruby, since there’s no way to make a variable local  
> > in scope to a function, but persistant in state over all  
> > calls to that function (at least that I know of).
