From: Joel VanderWerf Date: 2006-04-02T05:27:11+09:00 Subject: Re: (Static) Constructors/Destructors in Ruby Victor Shepelev wrote: > Hmmm... Not so beautiful/springy day in Kharkov, Ukraine :) Nor in Northern California. It's so wet the sidewalks are turning green. >> 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? Here's an idea: require 'socket' class ResourceMgr def [](*resources) @resources = resources end def cleanup @resources.reverse_each do |resource, cleanup_method| resource.__send__ cleanup_method || :close end end end def with_resources res = ResourceMgr.new yield res ensure res.cleanup end rl_test = nil with_resources do |res| res[ f = File.open("/tmp/file", "w"), s = UDPSocket.open, [t = Thread.new {sleep}, :kill] ] rl_test = [f,s,t] p rl_test # use resources here end p rl_test __END__ Output: [#, #, [#, :kill]] [#, #, [#, :kill]] -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407