From: "Hal E. Fulton" Date: 2001-09-08T00:47:47+09:00 Subject: [ruby-talk:20983] Re: destructor ----- Original Message ----- From: Nat Pryce To: ruby-talk ML Sent: Friday, September 07, 2001 6:34 AM Subject: [ruby-talk:20978] Re: destructor > Ruby uses blocks to scope resource allocation and deallocation to scopes , > just like C++ does with constructors/destructors. Look at the File::open > method for an example. > > The basic idiom is for the class to define a method that takes a block, > create an instance, pass it to the block, and then destroy the instance with > an ensure statement. > > Here's some pseudocode illustrating this idiom in the abstract... > > class Resource > def Resource.use( param ) > resource = Resource.new( param ) > try > return yield( resource ) > ensure > resource.close > end > end > end That's an excellent way of explaining it. Hal