> { > // Setup resource. > StackObject stack_object; > // Do stuff with the mode being setup by the stack object. > // Tear down happens automatically at block exit. > } > > However, a very similar idea is possible in Ruby, and can be achieved > with passing code blocks... > > def run_within_mode > setup_code > yield > ensure > teardown_code > end > > And to use: > > > # Here, the mode is not in use yet. > run_within_mode do > # Anything here is happening with the mode! Hooray! > end > # And the mode's gone again. > > As well as ensuring the resource is cleaned up, both of these > approaches are exception safe too. 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: ??? > > Cheers, > Ben Victor.