From: Benjohn Barnes Date: 2006-04-01T19:25:17+09:00 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 :) 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. Cheers, Ben