Multiple Ensure Blocks

I'm considering raising an RCR: "allow multiple ensure blocks". I've
searched for this to some extent - have I missed anything obvious?

I want to be able to write:

    begin
        raise 'something bad'
    ensure
        puts 1
    ensure
        puts 2
    end

This is so I can reliably cleanup all request resources in my fcgi
scripts (rollback database connection, finish the request, cleanup
environment and session objects) without having to nest
begin...ensure...end blocks inside the only ensure block I'm allowed
to write.

I know that the Ruby Way is probably to nest separate blocks for each
of these resources

    Database.use { |dbh|
        Request.use { |req|
            Session.use { |ses|
                ...
                # my code here
            }
        }
    }

but I end up nesting the real code about 10 layers deep if I do that.

Graham Jenkins wrote:

I'm considering raising an RCR: "allow multiple ensure blocks". I've
searched for this to some extent - have I missed anything obvious?

I want to be able to write:

    begin
        raise 'something bad'
    ensure
        puts 1
    ensure
        puts 2
    end

How does this differ from:

   begin
     raise 'something bad'
   ensure
     puts 1
     puts 2
   end

?

- Jamis

···

This is so I can reliably cleanup all request resources in my fcgi
scripts (rollback database connection, finish the request, cleanup
environment and session objects) without having to nest
begin...ensure...end blocks inside the only ensure block I'm allowed
to write.

I know that the Ruby Way is probably to nest separate blocks for each
of these resources

    Database.use { |dbh|
        Request.use { |req|
            Session.use { |ses|
                ...
                # my code here
            }
        }
    }

but I end up nesting the real code about 10 layers deep if I do that.

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

Make a wrapper:

class Session
   def self.environment
  Database.use do |dbh|
    Request.use do |req|
    Session.use do |ses|
      yield dbh, req, ses
    end
    end
  end
   end
end

Session.environment do |dbh, req, ses|
end

···

On 03 Dec 2004, at 10:08, Graham Jenkins wrote:

I know that the Ruby Way is probably to nest separate blocks for each
of these resources

    Database.use { |dbh|
        Request.use { |req|
            Session.use { |ses|
                ...
                # my code here
            }
        }
    }

but I end up nesting the real code about 10 layers deep if I do that.

If 'puts 1' throws an exception...

Gavin

···

On Saturday, December 4, 2004, 5:16:22 AM, Jamis wrote:

Graham Jenkins wrote:

I'm considering raising an RCR: "allow multiple ensure blocks". I've
searched for this to some extent - have I missed anything obvious?

I want to be able to write:

    begin
        raise 'something bad'
    ensure
        puts 1
    ensure
        puts 2
    end

How does this differ from:

   begin
     raise 'something bad'
   ensure
     puts 1
     puts 2
   end

?