# Re: Ruby and capturing ^C

**URL:** <https://www.ruby-forum.com/t/re-ruby-and-capturing-c/91161>\
**Category:** Ruby\
**Created:** [April 5, 2007, 8:16am UTC](https://www.ruby-forum.com/t/re-ruby-and-capturing-c/91161 "2007-04-05T08:16:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [April 5, 2007, 8:16am UTC](https://www.ruby-forum.com/t/re-ruby-and-capturing-c/91161/1 "2007-04-05T08:16:06Z")

</div>

Umm, I guess my searching was just plain bad. Thanks for being kind to  
me though. Mucho gracias.

Phy

----- Original Message ----  
From: Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, April 4, 2007 10:47:20 PM  
Subject: Re: Ruby and capturing ^C

On 4/5/07, Phy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello!
> 
> Is there any way to capture or “trap” ^C within Ruby? Something similar to shell “trapping” is what I am after so that I can do clean up in the event ^C is pressed.
> 
> TIA!  
> Phy

It’s one thing when you don’t know what to search for, but did you even  
try?  
[http://www.google.com/search?hl=en&q=ruby+trap&btnG=Google+Search](http://www.google.com/search?hl=en&q=ruby+trap&btnG=Google+Search)

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [April 5, 2007, 8:21am UTC](https://www.ruby-forum.com/t/re-ruby-and-capturing-c/91161/2 "2007-04-05T08:21:07Z")

</div>

How can I remove such a trapping from a program?

I’m running autotest through a rake task. Autotest uses ^C to  
restart the tests, rake uses it to cancel the task at hand.

What would be the best way to remove this trap from rake?

Best,

Scott T.

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [April 5, 2007, 8:37am UTC](https://www.ruby-forum.com/t/re-ruby-and-capturing-c/91161/3 "2007-04-05T08:37:08Z")

</div>

On Thursday 05 April 2007, Scott T. wrote:

> How can I remove such a trapping from a program?
> 
> I’m running autotest through a rake task. Autotest uses ^C to  
> restart the tests, rake uses it to cancel the task at hand.
> 
> What would be the best way to remove this trap from rake?  
> Under Posix, you would have to use sigmask(). I don’t think there is a  
> ruby  
> equivalent though …

Note that ^C is already trapped by the interpreter, which raises  
Interrupt …

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [April 5, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/re-ruby-and-capturing-c/91161/4 "2007-04-05T21:05:30Z")

</div>

Scott T. wrote:

> How can I remove such a trapping from a program?
> 
> I’m running autotest through a rake task. Autotest uses ^C to restart  
> the tests, rake uses it to cancel the task at hand.
> 
> What would be the best way to remove this trap from rake?

I don’t know if this will help without hacking into rake or autotest,  
but you can save and restore the handler. The return value of #trap is  
the previous handler, which is just a proc object:

irb(main):001:0\> oldh = trap(“INT”) {puts “newh”}  
=\> #Proc:0x02b2ac30@c:/ruby/lib/ruby/1.8/irb.rb:65  
irb(main):002:0\> ### \<-- I pressed ^C and Enter here  
newh  
irb(main):003:0\*  
irb(main):004:0\* newh = trap(“INT”, &oldh)  
=\> #Proc:0x02e14428@:1(irb)  
irb(main):005:0\>  
^C

There is also a useful special case. If you pass “DEFAULT” as the  
handler, you go back to ruby’s default (which is different from irb’s  
handler):

irb(main):001:0\> trap(“INT”, “DEFAULT”)  
=\> #Proc:0x02b2ac30@c:/ruby/lib/ruby/1.8/irb.rb:65  
irb(main):002:0\> ### \<-- I pressed ^C and Enter here  
c:/ruby/lib/ruby/1.8/irb/input-method.rb:97:in `gets': Interrupt from c:/ruby/lib/ruby/1.8/irb.rb:132:in`eval\_input’  
from c:/ruby/lib/ruby/1.8/irb.rb:259:in `signal_status' from c:/ruby/lib/ruby/1.8/irb.rb:131:in`eval\_input’  
from c:/ruby/lib/ruby/1.8/irb/ruby-lex.rb:189:in `call' from c:/ruby/lib/ruby/1.8/irb/ruby-lex.rb:189:in`buf\_input’  
from c:/ruby/lib/ruby/1.8/irb/ruby-lex.rb:104:in `getc' from c:/ruby/lib/ruby/1.8/irb/slex.rb:206:in`match\_io’  
from c:/ruby/lib/ruby/1.8/irb/slex.rb:76:in `match' ... 8 levels... from c:/ruby/lib/ruby/1.8/irb.rb:70:in`start’  
from c:/ruby/lib/ruby/1.8/irb.rb:69:in `catch' from c:/ruby/lib/ruby/1.8/irb.rb:69:in`start’  
from c:/ruby/bin/irb.bat:20  
Terminate batch job (Y/N)? y
