Joel VanderWerf wrote:
> Ken Bloom wrote:
>> On Tue, 15 Apr 2008 11:28:33 -0500, Daniel Berger wrote:
>>
>>> ts wrote:
>>>> Daniel Berger wrote:
>>>>> # process_test.rb
>>>>> File.open("pid.txt", "w"){ |fh| fh.print Process.pid } p Process.pid
>>>>>
>>>>> sleep 1 while true
>>>>  you really think that the following line will be executed ?
>>> Why not? The docs say at_exit, "Converts block to a Proc object (and
>>> therefore binds it at the point of call), and registers it for execution
>>> when the program exits."
>>
>> Ruby does *everything* in the order it encounters it in the file.
>> at_exit installs an exit handler, (a proc to be run later) but it's a 
>> method that has to be executed. If you put "sleep 1 while true" before 
>> the at_exit call, the at_exit call will never be executed, so the exit 
>> handler will never be installed. Adjust your program to call at_exit 
>> (or whatever other handlers you want) before running anything else.
> 
> Note that you can use BEGIN to register handlers out of order:
> 
> exit
> 
> BEGIN {
>   at_exit { puts "at_exit" }
>   END { puts "END" }
> }
> 
> 
> (without the BEGIN, nothing is printed)

Ah, yes. I think between this and Tim's all purpose catcher, we have the 
makings of a nice little library. :)

Many thanks,

Dan