From: Josef Wolf Date: 2006-09-07T02:10:25+09:00 Subject: Re: How to store/load persistent data? On Wed, Sep 06, 2006 at 07:17:46AM +0900, Timothy Hunter wrote: > Josef Wolf wrote: > There is a well-known Ruby idiom for coding block-scoped resources. > There used to be a good page describing this in RubyGarden but alas that > page seems to have disappeared. Anyway, here's the general example from > that page: > > > def Resource.open( identifier ) # :yield: resource > > resource = Resource.new( identifier ) > > if block_given? > > begin > > yield resource > > ensure > > resource.close > > end > > else > > return resource > > end > > end > > That is, create/open/whatever the resource is and yield to a block. > Within the block use the resource. When the block returns, close/destroy > the resource. The open method in the IO class is prototypical. I am aware of this idiom. This idiom works very well if you have a strictly hierachical call structure. But I just can't see how this can help when dealing with Tk (especially with canvas items). In this case, objects are created and destroyed asynchronously. The block that creates a new widget has to return _before_ the widget is destroyed. Of course, with widgets like TkDialog, this idiom would work very well since dialog->Show() blocks until the widget is destroyed. OTOH, TkDialog don't need this idiom since it is already destroyed when its Show method returns.