# OT: Ruby question: alternative to begin/ensure block

**URL:** <https://www.ruby-forum.com/t/ot-ruby-question-alternative-to-begin-ensure-block/141800>\
**Category:** Rails\
**Created:** [June 30, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/ot-ruby-question-alternative-to-begin-ensure-block/141800 "2008-06-30T18:12:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [June 30, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/ot-ruby-question-alternative-to-begin-ensure-block/141800/1 "2008-06-30T18:12:30Z")

</div>

Sorry for asking a purely Ruby question, but somebody on the list  
knows the answer to this.

Are there any alternatives to begin/ensure blocks that can be used to  
ensure that specific cleanup code gets executed?

In my particular case, I am interacting with an Excel spreadsheet  
using OLE with something like:

require ‘win32ole’  
xl = WIN32OLE.new(‘Excel.Application’)  
begin  
do\_all\_of\_my\_spreadsheet\_processing\_here  
ensure  
xl.Workbooks.each { |w| w.Close(false) }  
xl.Quit()  
end

I want to make sure that I close all of the workbooks w/o saving, and  
that the Excel application terminates if any exceptions are raised.

Using begin/ensure solves this at the expense of an added level of  
indentation. What I would like to do is something along the lines of

xl = WIN32OLE.new(‘Excel.Application’)  
xl.before\_going\_out\_of\_scope do |x|

# Cleanup connection to the Excel application so that it exits

x.Workbooks.each { |w| w.Close(false) }  
x.Quit()  
end

This has the advantage (in my mind) of keeping the cleanup code right  
next to the allocation code and allows me to forget all about it as I  
continue on with my application.

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [June 30, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/ot-ruby-question-alternative-to-begin-ensure-block/141800/2 "2008-06-30T18:33:35Z")

</div>

On 30 Jun 2008, at 17:11, Patrick D. wrote:

> Sorry for asking a purely Ruby question, but somebody on the list  
> knows the answer to this.
> 
> Are there any alternatives to begin/ensure blocks that can be used to  
> ensure that specific cleanup code gets executed?

AFAIK has to been begin/ensure (finalizers wouldn’t do the trick here)

That’s not to say you can’t make things a little nicer (similar to  
File.open):

class Excel  
def self.do\_stuff  
xl = WIN32OLE.new(‘Excel.Application’)  
yield xl  
ensure  
xl.Workbooks.each { |w| w.Close(false) }  
xl.Quit()  
end  
end

Then you can do

Excel.do\_stuff do |xl|  
…  
end

Fred

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [June 30, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/ot-ruby-question-alternative-to-begin-ensure-block/141800/3 "2008-06-30T18:52:21Z")

</div>

On Mon, Jun 30, 2008 at 12:33 PM, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 30 Jun 2008, at 17:11, Patrick D. wrote:
> 
> > Sorry for asking a purely Ruby question, but somebody on the list  
> > knows the answer to this.
> > 
> > Are there any alternatives to begin/ensure blocks that can be used to  
> > ensure that specific cleanup code gets executed?
> 
> AFAIK has to been begin/ensure (finalizers wouldn’t do the trick here)  
> Ahhh… “finalizers” look like what I wanted to use, although, as you  
> point out, they wouldn’t do the trick here, since they don’t have  
> access to the underlying object.

> ```
> xl.Quit()
> 
> ```
> 
> end  
> end
> 
> Then you can do
> 
> Excel.do\_stuff do |xl|  
> …  
> end

Thank you. I thought of doing something like that, but wrapped myself  
around a pole mistakenly trying to figure out how extend the WIN32OLE  
class to do something like this. You’re solution is much more elegant  
and solves the problem of placing the “cleanup” code next to the  
“open” code, but doesn’t solve the indentation problem. I think I’ll  
give it a shot.

Onto a slightly different topic…  
I’ve noticed your name next to a lot of responses to newbie questions  
from myself and others. I want to say thank you very much for taking  
the time and effort to help us all along.

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [June 30, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/ot-ruby-question-alternative-to-begin-ensure-block/141800/4 "2008-06-30T19:18:49Z")

</div>

> I’m not sure I get the ‘indentation problem’. To me it almost sounds  
> like you’re saying ‘Well an if statement sure would do the trick, but  
> then i’d have to indent stuff’. If you hate the indentation that much  
> then just don’t indent (although personally I think that will make  
> your code look a mess.

The “indentation problem” is one of excessive indentation, which can  
always be solved by factoring the code. It just seems that the use of  
a begin/ensure block, a File.open-like block, and even an if block,  
all result in the rest of my code being indented a level uselessly.  
Imagine if I had an Excel worksheet, and a Word document, and an  
ActiveRecord database, etc… all competing for cleanup clauses (well,  
perhaps it’s a stretch with the ActiveRecord one…). I was just  
hoping that magical Ruby would have some sort of:

object.before\_going\_out\_of\_scope do |x|  
x.blah  
end

incantation, that would add a callback to the object. But perhaps  
that adds too much overhead, and not enough feature to be of use  
outside my little project.

(I know you said not to mention it, but I’m going to mention it anyway)  
Thanks again.

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [June 30, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/ot-ruby-question-alternative-to-begin-ensure-block/141800/5 "2008-06-30T19:00:34Z")

</div>

On 30 Jun 2008, at 17:51, Patrick D. wrote:

> > > 
> > 
> > AFAIK has to been begin/ensure (finalizers wouldn’t do the trick  
> > here)  
> > Ahhh… “finalizers” look like what I wanted to use, although, as you  
> > point out, they wouldn’t do the trick here, since they don’t have  
> > access to the underlying object.

They can do, but it still doesn’t help, because you have no control  
over when they run. If ruby never needs to garbage collect (and it  
will only do so if memory usage hits a certain threshold) your  
finalizers will never run

> > ensure
> 
> Thank you. I thought of doing something like that, but wrapped myself  
> around a pole mistakenly trying to figure out how extend the WIN32OLE  
> class to do something like this. You’re solution is much more elegant  
> and solves the problem of placing the “cleanup” code next to the  
> “open” code, but doesn’t solve the indentation problem. I think I’ll  
> give it a shot.

I’m not sure I get the ‘indentation problem’. To me it almost sounds  
like you’re saying ‘Well an if statement sure would do the trick, but  
then i’d have to indent stuff’. If you hate the indentation that much  
then just don’t indent (although personally I think that will make  
your code look a mess.

Fred

> Onto a slightly different topic…  
> I’ve noticed your name next to a lot of responses to newbie questions  
> from myself and others. I want to say thank you very much for taking  
> the time and effort to help us all along.

don’t mention it.
