[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Multiple Ensure Blocks

gjenkins

12/3/2004 6:05:00 PM

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.
3 Answers

Jamis Buck

12/3/2004 6:16:00 PM

0

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


Eric Hodel

12/3/2004 7:02:00 PM

0

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.

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



Gavin Sinclair

12/4/2004 1:15:00 AM

0

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

> ?

If 'puts 1' throws an exception...

Gavin