Yukihiro Matsumoto wrote:

> Hi,
> 
> In message "[ruby-talk:26285] Re: [OT] Re: KDE or GNOME curiosity
> question..."
>     on 01/11/24, Edward Diener <eldiener / earthlink.net> writes:
> 
> |> Ruby and many other languages with GC (Java is in the same boat) allow
> |> resource finalization when the object is "destroyed".
> |
> |I am aware of that but unfortunately, like Java, one can't force the
> |object to be "destroyed" a well-ordered point.
> 
> I don't get the point.  First of all, what does RAAI stand for?

This page incorrectly refers to it as a C++ idiom:

  http://c2.com/cgi/wiki?ResourceAcquisitionIsInitialization

Just because C++ is the only language deterministic enough to do it...

> Second, are you claiming either following (a) or (b)?
> 
>  (a) resource finalization must occur when the object is recycled.

Yes, for C++ style RAII.
 
>  (b) object reclamation must occur when the last reference to it is
>      removed.

Yes, for C++ style RAII.

Read /Exceptional C++/ by Herb Sutter. If you expect exceptions (in any 
language) to not leave a program in an intermediate or undetermined state, 
you must either roll the state back in the destructors that call when your 
objects go out of scope, or you must 'ensure' that state rolls back.

RAII is better than 'ensure' blocks because the latter forces the calling 
code to be aware of what this state is, to roll it back. This couples the 
calling code to the called code.

> Ruby ensures (a), not (b).  It's kinda hard to tell when the
> reclamation happens.  But I believe if you want to finalize
> resource/object at certain point, you'd better to finalize it
> explicitly.  If you don't care when, leave it to the garbage
> collector.

> Plus, for explicit resource management, in Ruby, you can use the idiom
> like
> 
>   open(path) do |f|
>     ...
>   end
> 
> that ensures f to be closed at the end of the block.

This is also better than a visible 'ensure' because it implies that 'f' 
rolled its state back at exception time.

But may I ask if  this implies that the code inside 'open' that yielded to 
the lambda was itself inside of a 'begin ensure' block?

If so, this satisfies RAII for Ruby.

-- 
  Phlip                          phlip_cpp / yahoo.com
           http://sourceforge.net/projects/flea
  --  "In my experience, the customer doesn't know what he wants
       until you don't give it to him." --David Brady  --

http://www.greencheese.org/HatTrick