Multiple Ensure Blocks

G

Graham Jenkins

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.
 
J

Jamis Buck

Graham said:
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
 
E

Eric Hodel

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top