[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Break loop by trapping INT

Todd A. Jacobs

9/21/2007 11:04:00 PM

I'm trying to break a loop whenever CTRL-C is pressed, and find that
this doesn't work:

trap 'INT', proc {break}; count=0; while count < 10;
puts count += 1; sleep 1; end

I'm guessing it's because the loop is somehow out of scope, but I'm not
sure why.

Some minor syntax changes to trap makes it work, sort of. If I use:

trap('INT') {break}

instead, then I get a LocalJumpError. So, why does the syntax only work
one way, and what is the right way to trap the interrupt?

--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"

3 Answers

Morton Goldberg

9/22/2007 12:56:00 AM

0

On Sep 21, 2007, at 7:04 PM, Todd A. Jacobs wrote:

> I'm trying to break a loop whenever CTRL-C is pressed, and find that
> this doesn't work:
>
> trap 'INT', proc {break}; count=0; while count < 10;
> puts count += 1; sleep 1; end
>
> I'm guessing it's because the loop is somehow out of scope, but I'm
> not
> sure why.
>
> Some minor syntax changes to trap makes it work, sort of. If I use:
>
> trap('INT') {break}
>
> instead, then I get a LocalJumpError. So, why does the syntax only
> work
> one way, and what is the right way to trap the interrupt?

When the trap is triggered, the trap's proc object receives a 'call'
message. This happens in the scope of top level, not in the scope of
the while block. To get the result you want, you need to invoke a non-
local jump mechanism. This can be done with catch and throw.

<code>
trap('INT') { puts "done"; throw :quit }

count = 0
catch :quit do
while count < 10;
puts count += 1
sleep 1
end
end
</code>

Regards, Morton

7stud 7stud

9/22/2007 2:13:00 AM

0

Morton Goldberg wrote:
> When the trap is triggered, the trap's proc object receives a 'call'
> message. This happens in the scope of top level, not in the scope of
> the while block. To get the result you want, you need to invoke a non-
> local jump mechanism. This can be done with catch and throw.
>
> trap('INT') { puts "done"; throw :quit }
>
> count = 0
> catch :quit do
> while count < 10;
> puts count += 1
> sleep 1
> end
> end

Yikes! Isn't that using catch/throw as a plain vanilla goto?
--
Posted via http://www.ruby-....

Morton Goldberg

9/22/2007 1:38:00 PM

0

On Sep 21, 2007, at 10:13 PM, 7stud -- wrote:

> Morton Goldberg wrote:
>> When the trap is triggered, the trap's proc object receives a 'call'
>> message. This happens in the scope of top level, not in the scope of
>> the while block. To get the result you want, you need to invoke a
>> non-
>> local jump mechanism. This can be done with catch and throw.
>>
>> trap('INT') { puts "done"; throw :quit }
>>
>> count = 0
>> catch :quit do
>> while count < 10;
>> puts count += 1
>> sleep 1
>> end
>> end
>
> Yikes! Isn't that using catch/throw as a plain vanilla goto?

Not quite. Catches have scope. Consider the following:

<code>
trap('INT') { puts "done"; throw :quit }

count = 0
catch :quit do
while count < 10;
catch :next do ; end
puts count += 1
sleep 1
end
end
puts "But I don't want to quit!"
throw :next
</code>

That doesn't work because catch :next is invisible to throw :next.
Throws only search upward in the stack frames, not downward. Now this
could be made to work with continuations. With continuations you can
do truly shocking things.

But even if were just like a goto, so what? Using trap already
injects non-local behavior into the script. A little catch and throw
hardly matters afters that. And it gets the job done. In this
particular case, because there is nothing to do after the while
block, the OP could use:

<code>
trap('INT') { puts "done"; exit }

count = 0
while count < 10;
puts count += 1
sleep 1
end
</code>

But that's not really very different, only less general.

Regards, Morton