From: Jim Weirich Date: 2003-05-20T13:57:49+09:00 Subject: Re: An Object Going Out Of Scope On Mon, 2003-05-19 at 11:55, MikkelFJ wrote: > 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 How about this syntax ... auto(foo=MyObject.new, :cleanup) do # use foo end The above can be written in standard Ruby. > 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; .... } > Critcal sections are better handled by blocks ... lock(mutex) do # critical section end -- -- Jim Weirich jweirich@one.net http://jimweirich.umlcoop.net --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)