static local variables

J

jdm

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

Austin Ziegler

i'm trying to create what C calls a static local variable inside a partic= ular
method, i.e. this variable should not be visible outside the method and i= ts
value should survive the activation of the method.

instance variables are "static" but visible to all methods of the instanc= e.

local variables are visible only to a particular method but aren't "stati= c".

i've been through the pickaxe book and nothing obvious popped out at me. = it
seems i could synthesize the desired functionality using singleton method= s
attached to a particular instance (or maybe by defining a block within th= e
method) but i'm hoping there's a simpler, cleaner way.

There is no equivalent in Ruby. Use instance variables.

-austin
 
1

1337p337

While there isn't a direct equivalent, you can do something similar.

def fibonacci_generator
=09current =3D 1
=09previous =3D 0
=09lambda {
=09=09tmp =3D previous
=09=09previous =3D current
=09=09current =3D tmp + previous
=09}
end

You can use it like this:

irb(main):015:0> fib =3D fibonacci_generator
=3D> #<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 i=
t,
you can get something similar to a static variable. Unlike C, though, you =
can
have multiple instances of the proc.
 
E

ES

jdm said:
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
 
L

Logan Capaldo

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.

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?
 
R

Robert Klemme

jdm said:
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
 
N

nobu.nokada

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top