Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detecting control-c?

87 views
Skip to first unread message

William E. Rubin

unread,
Dec 8, 2005, 2:36:07 PM12/8/05
to
How does one detect Control-C?

I know that it raises an Interrupt, but there are other things that are
also Interrupts (such as Timeout::Error). The documentation at
ruby-doc.org doesn't list any details of class Interrupt, and the
"Programming Ruby" book at ruby-lang.org doesn't even seem to mention
that class Interrupt exists.

Thanks.

Daniel Berger

unread,
Dec 8, 2005, 2:56:41 PM12/8/05
to
William E. Rubin wrote:
> How does one detect Control-C?

trap("INT"){ ... }

Interrupt is a subclass of SignalException (which is a subclass of Exception).
As far as I can tell (based on what I see in eval.c) it's only used for
trapping Unix signals*, i.e. whatever's in your signal.h file.

Also, take a look at the Signal module.

Regards,

Dan

* Works on Windows too, though the implementation is different, and likely
requires a separate sleeper thread.


Joe Van Dyk

unread,
Dec 8, 2005, 3:03:58 PM12/8/05
to

hump:[]:/home/mz652c% cat signal.rb

trap("INT") do
puts "got signal INT"
end

puts "Sup"
gets


hump:[]:/home/mz652c% ruby signal.rb
Sup
got signal INT
got signal INT
got signal INT
got signal INT
got signal INT


pressing ctrl-c generates 'got signal INT'. Had to press ctrl-d to
exit the program.


Jim Freeze

unread,
Dec 9, 2005, 8:50:11 AM12/9/05
to
On 12/8/05, Joe Van Dyk <joev...@gmail.com> wrote:
> trap("INT") do
> puts "got signal INT"
> end
>
> puts "Sup"
> gets
>
> hump:[]:/home/mz652c% ruby signal.rb
> Sup
> got signal INT
> got signal INT

Interesting. When I ran the program above I get:
^Cgot signal INT
...

Any ideas on how to not get the '^C' text?

--
Jim Freeze


Joe Van Dyk

unread,
Dec 9, 2005, 12:41:33 PM12/9/05
to
On 12/9/05, Jim Freeze <j...@freeze.org> wrote:
> On 12/8/05, Joe Van Dyk <joev...@gmail.com> wrote:
> > trap("INT") do
> > puts "got signal INT"
> > end
> >
> > puts "Sup"
> > gets
> >
> > hump:[]:/home/mz652c% ruby signal.rb
> > Sup
> > got signal INT
> > got signal INT
>
> Interesting. When I ran the program above I get:
> ^Cgot signal INT
> ....

>
> Any ideas on how to not get the '^C' text?

Dunno, I didn't get the '^C' text. (on linux)


gwt...@mac.com

unread,
Dec 9, 2005, 1:40:41 PM12/9/05
to

On Dec 9, 2005, at 8:50 AM, Jim Freeze wrote:

> On 12/8/05, Joe Van Dyk <joev...@gmail.com> wrote:
>> trap("INT") do
>> puts "got signal INT"
>> end
>>
>> puts "Sup"
>> gets
>>
>> hump:[]:/home/mz652c% ruby signal.rb
>> Sup
>> got signal INT
>> got signal INT
>
> Interesting. When I ran the program above I get:
> ^Cgot signal INT

> ....


>
> Any ideas on how to not get the '^C' text?

The ^C is your ctrl-c being echoed by the
terminal device driver. You can use the stty
program to alter the behavior of the tty driver.
In particular, take a look at the -echoctl
option. In any case, the foreground process (ruby) doesn't
see the ctrl-c because the tty driver discards
it and sends the Interrupt signal instead and then takes
car of echoing '^C' back to the terminal itself.

The specific behavior may also depend on the particular
shell you are using.

Hope this helps.

0 new messages