From: nobu.nokada@... Date: 2002-07-04T00:46:38+09:00 Subject: Re: Ruby implementation Q's Hi, At Wed, 3 Jul 2002 21:53:18 +0900, Justin Johnson wrote: > a = MyClass.new > Action: Instance of 'MyClass' is created and assigned to new local > variable 'a'. 'initialize' method is called for 'a'. No method called for a variable, for an object referred by 'a'. > a.destroy > Action: 'unitialize' method is called for 'a'. 'a' is removed from local > variable hash. 'a' no longer exists as a variable. Still the object referred by 'a' may exist. > Any ptr's that used to reference to 'a' are set to Nil. This can be done as > the runtime comes across ptr's to dead objects. This means that 'destroy' > could be implemented as a method - it wouldn't be possible to make 'a' live > again after 'destroy'. > > I'll admit, it's an unusal approach for gc languages to implement voluntary > destruction. > > The preference would be for finalization to be called as soon as an object > becomes unreferenced although I haven't thought of a good way of doing that. Probably, you want a way like allocation-by-construction in C++, the strategy is effective in the case of a variable is equivalent to an object, but unavailable for Ruby, Java and such languages whose variables are references, without reference count or similar. You need to use 'ensure(in Ruby)' and 'finally(in Java)' instead. def MyClass.use yield(obj = new) ensure obj.destroy end MyClass.ues do |a| a.do_something # may raise exceptions, end # but ensured to be destroyed -- Nobu Nakada