Static local variables

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.

There is no equivalent in Ruby. Use instance variables.

-austin

···

On 9/22/05, jdm <xyz@xyz.com> 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.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

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
=> #<Proc:0x00002aaaab22c9c0@(irb):4>
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 <xyz@xyz.com> 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.

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.

I would recommend just creating an object of some sort but if you really
need to, you can use instance variables of the enclosing scope (even the
top-level).

def foo(x)
   # Use the existing one if any or initialize
   @__foo_var ||= initializer
   do_something_with x, @__foo_var
end

E

···

--
No-one expects the Solaris POSIX implementation!

Closures are your friends

% cat a.rb
class A
     def meth1
            puts "I'm meth1"
      end
      def meth2
           puts "I'm meth2. I try to access something I ain't allowed: #{static_var}"
      end
       class_eval do
             static_var = 3
             define_method(:meth3) do
                      puts "I'm meth3. I'm allowed to access #{static_var}"
             end
       end
end

a = A.new
a.meth1
a.meth3
a.meth2

% ruby a.rb
I'm meth1
I'm meth3. I'm allowed to access 3
a.rb:6:in `meth2': undefined local variable or method `static_var' for #<A:0x26148> (NameError)
         from a.rb:19

Personally I think C's static vars are cheesy attempt to provide the a tiny amount of the power of lexicaly scoped closures in a language with manual memory management.

In this case I don't know if a variable with this kind of scope is the best choice however, are you sure you can't make the method into an object?

···

On Sep 22, 2005, at 8:31 PM, 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.

Just don't do it. As you see attempts to simulate this queer feature of C soon get ugly and bloated. If you need something like this rather think about command pattern (i.e. basically you create a class for a single, usually complex task).

Kind regards

    robert

···

jdm <xyz@xyz.com> 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.

Hi,

At Fri, 23 Sep 2005 09:31:39 +0900,
jdm wrote in [ruby-talk:157197]:

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.

I'd suggested EVAL_ONCE but not accepted yet.

Only thing evaluated once is regexp with o option, so possible
alternative is using instance variables of Regexp.

def foo
  static = //o
  svar = static.instance_eval {
    @foo ||= some_method
  }
end

···

--
Nobu Nakada