From: 1337p337 <1337p337@...> Date: 2005-09-23T11:54:22+09:00 Subject: Re: static local variables While there isn't a direct equivalent, you can do something similar. def fibonacci_generator current = 1 previous = 0 lambda { tmp = previous previous = current current = tmp + previous } end You can use it like this: irb(main):015:0> fib = fibonacci_generator => # irb(main):016:0> 10.times { puts fib.call } 1 2 3 5 8 13 21 34 55 89 Basically, by returning a proc that contains local state and then calling it, you can get something similar to a static variable. Unlike C, though, you can have multiple instances of the proc. On 22/09/05, jdm wrote: > i'm trying to create what C calls a static local variable inside a particular > method, i.e. this variable should not be visible outside the method and its > value should survive the activation of the method. > > instance variables are "static" but visible to all methods of the instance. > > local variables are visible only to a particular method but aren't "static". > > i've been through the pickaxe book and nothing obvious popped out at me. it > seems i could synthesize the desired functionality using singleton methods > attached to a particular instance (or maybe by defining a block within the > method) but i'm hoping there's a simpler, cleaner way. > > thanx in advance. > > > >