"Dave Thomas" <Dave / PragmaticProgrammer.com> wrote in message
news:m2zo1zzq45.fsf / zip.local.thomases.com...
> "Sean O'Dell" <sean / celsoft.com> writes:
>
> > I considered using "yield" to wrap things up (using block_given? to
enforce
> > using blocks, throwing an exception when a block wasn't provided) to
ensure
> > this happens, but that got really messy fast in one of my apps.  I had
about
> > 6 objects to create, all of which required special destruction tasks,
and
> > that meant I had 6-level-deep nested block, complete with 6 calls to
yield.
> > The code looks horrible.  It's so much cleaner when you can create 6
objects
> > in succession, and they destruct in reverse order.  I could wrap all 6
> > objects inside a block with an ensure clause and then call their
destructors
> > explicitly, but that requires that the outside world know about the
> > destructors, and that ruins the whole idea of encapsulation.
>
> How about having a simple "managed object pool". Then you could write

That's an idea.  Something like this might work:

module MyApp
    def safefunction
        raise "no block given" if(not block_given?)
        Object.call do |o1|
            Object.call do |o2|
                Object.call do |o3|
                    yield(o1, o2, o3)
                end
            end
        end
    end
end

MyApp::safefunction do |o1, o2, o3|
    # do stuff in here
end

    Sean