Hugh Sasse wrote: > On Sat, 26 Nov 2005, Daniel Schierbeck wrote: > >> Oh, sorry, I misunderstood you: You mean something like this: >> >> class Klass >> def initialize > # setup usual stuff, then: >> yield self >> finalize >> end >> >> def finalize >> # do something > # like releasing resources, yes. >> end >> end >> >> Sorry, I'm kinda groggy ;) > > Not that groggy, that's exactly what I meant. >> Cheers, >> Daniel >> > Hugh What would you call such an approach? Encapsulating? I use this often enough that I think it would fit lovely in a module: module Encapsulatable def self.included(klass) super klass.module_eval do def self.new(*args) yield obj = __new__(*args) obj.finalize if obj.respond_to? :finalize end end end end Cheers, Daniel