From: Victor Shepelev Date: 2006-04-01T19:36:57+09:00 Subject: Re: (Static) Constructors/Destructors in Ruby > -----Original Message----- > From: Benjohn Barnes [mailto:benjohn@fysh.org] > Sent: Saturday, April 01, 2006 1:25 PM > To: ruby-talk ML > Subject: Re: (Static) Constructors/Destructors in Ruby > > > On 1 Apr 2006, at 11:10, Victor Shepelev wrote: > > I like block idea, but must say that blocks can't give full > > replacement for > > C++'s RAII ideom: > > > > C++: > > > > { > > File f1,f2,f3; > > Socket s1,s2; > > Database d; > > } //here all f1,f2,f3,s1,s2,d being closed > > > > Ruby: > > > > ??? > > :) It's such a fresh beautiful day outside... and here I am talking > tech, when spring is springing :) Hmmm... Not so beautiful/springy day in Kharkov, Ukraine :) > Ok, in Ruby, each of those resources could easily be turned in to a > method that will run a block. What you want to be able to do then, is > easily compose those methods in to one, and have that run something > within the context of resources being available? > > Something along the lines of (unchecked code warning!)...? > > def run_with_resources(*resources, &block) > if resources == [] > yield > else > send(resources.slice(0)) {run_with_resources(*resources, > &block)} > end > end > > You can then do... > > run_with_resources > (:file_handle, :db_connection, :socket_and_things, :other_groovy_bits) d > o > #Wooo - look at all these resources! > end > > It wouldn't be a big extension to collect up the resources passed in, > and pop them in a map, or something. I don't think it would be hard > to give the resources arguments either. Yes, I know all those trick (I'm not a VERY newbie :) - my point was "blocks not always good alternative for RAII". BTW, I recalled I saw some library at RAA, which does the stuff. But I don't remember it's name (because in real life it is not as necessary as in dumb examples ;) > Cheers, > Ben Victor.