"Jim Weirich" <jweirich / one.net> wrote in message
news:1053260343.6876.36.camel / traken...
> On Sun, 2003-05-18 at 05:08, MikkelFJ wrote:
> >
> > "Jim Weirich" <jweirich / one.net> wrote in message
> > news:1053238069.6843.15.camel / traken...
> > > On Sun, 2003-05-18 at 00:47, vinita Papur wrote:
> > > > A quick question.  How can one discern when an object goes out of
scope?
> > > > I'd like to do something like this:
> > >
> > > Objects don't have scope.  Variables do.  (a subtle distinction)
> >
> > > Use a block to manage resources.  Like this ...
> >
> > Still, it would actually be nice to have a feature in Ruby that would
> > trigger a finalizer when the variable goes out of scope. There are
problems
> > becuase there could be more references at the time of finalization, but
this
> > could also  be the case with cleanup using structs. I.e. closing a file
> > doesn't guarantee there are no more references to the file.
>
> Just because a variable goes out of scope doesn't mean that its object
> is no longer used.  That's the whole reason we garbage collection in the
> first place, so the programmer no longer needs to worry about what
> references what.

(typo: I said struct above, I mean block)

I didn't go through all you examples, but I did say that you could still
have references at time of finalization.

In a block you have { |resource| ... }
When the block terminates, resource.cleanup is called. However, what
prevents you from storing references to the resource all over the place: {
|resource| $nasty_global = resource }?
Thus auto cleanup isn't more or less safe than using auto as in D.

The Ruby interpreter nows pretty well when the scope exits and it could
easily call a default method on all auto references. auto could even be
given a name, but probably not arguments. Never mind the syntax, but an
example could be:

{ auto(:foo); foo = Foo.new } # calling foo.finalize on scope exit
or
{ auto(:foo, :cleanup); foo = foo.new } # calling foo.cleanup on scope exit

Wrt.complex scope rules. The scope should not be for an arbitrary variable.
Some simple rules could be defined and the auto could work an a variable the
exists before and after the syntactical scope we are in.
It could even work on global variables. (This is actually useful for
critical sections and the like):

{ auto(:$critsec, :exit); @critsec.enter; .... }

Mikkel