# Catch ^c/signals? blocks?

**URL:** <https://www.ruby-forum.com/t/catch-c-signals-blocks/98668>\
**Category:** Ruby\
**Created:** [July 17, 2006, 11:43am UTC](https://www.ruby-forum.com/t/catch-c-signals-blocks/98668 "2006-07-17T11:43:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliteforce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@eliteforce](https://www.ruby-forum.com/u/eliteforce)\
**Post date:** [July 17, 2006, 11:43am UTC](https://www.ruby-forum.com/t/catch-c-signals-blocks/98668/1 "2006-07-17T11:43:44Z")

</div>

Hello,

I’m kinda new to ruby and have a few questions.

If i run a ruby script it can be interrupted with ^c or signals (e.g.  
killall ruby"). Is it possible to catch those to do cleanup stuff before  
exiting?

Whats the difference between { … } and do … end? Are they both  
blocks? When to use what?

I’d really appreciate it if you could answer my questions, thanks.

---

<div class="post-metadata">

**Author:** ![eliteforce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@eliteforce](https://www.ruby-forum.com/u/eliteforce)\
**Post date:** [July 17, 2006, 11:51am UTC](https://www.ruby-forum.com/t/catch-c-signals-blocks/98668/2 "2006-07-17T11:51:55Z")

</div>

Elite F. wrote:

> I’d really appreciate it if you could answer my questions, thanks.

I would recommend that you lookup either the method Kernel#trap, the  
class Signal, or maybe both?

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![eliteforce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@eliteforce](https://www.ruby-forum.com/u/eliteforce)\
**Post date:** [July 17, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/catch-c-signals-blocks/98668/3 "2006-07-17T12:09:05Z")

</div>

Ola B. wrote:

> Elite F. wrote:
> 
> > I’d really appreciate it if you could answer my questions, thanks.
> 
> I would recommend that you lookup either the method Kernel#trap, the  
> class Signal, or maybe both?
> 
> –  
> Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
> JvYAML, RbYAML, JRuby and Jatha contributor  
> System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
> OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))
> 
> “Yields falsehood when quined” yields falsehood when quined.

does ctrl-c also send a signal to the process? (i guess so)

---

<div class="post-metadata">

**Author:** ![eliteforce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@eliteforce](https://www.ruby-forum.com/u/eliteforce)\
**Post date:** [July 17, 2006, 12:48pm UTC](https://www.ruby-forum.com/t/catch-c-signals-blocks/98668/4 "2006-07-17T12:48:44Z")

</div>

Ola B. wrote:

> Elite F. wrote:
> 
> > does ctrl-c also send a signal to the process? (i guess so)
> 
> Yes, C-c generates a SIGINT in most common operating systems.

Okay, thank you very much!

So my last question left open is, if there’s a difference between do end  
and { } blocks

---

<div class="post-metadata">

**Author:** ![eliteforce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@eliteforce](https://www.ruby-forum.com/u/eliteforce)\
**Post date:** [July 17, 2006, 12:32pm UTC](https://www.ruby-forum.com/t/catch-c-signals-blocks/98668/5 "2006-07-17T12:32:04Z")

</div>

Elite F. wrote:

> > System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
> > OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))
> > 
> > “Yields falsehood when quined” yields falsehood when quined.
> 
> does ctrl-c also send a signal to the process? (i guess so)

Yes, C-c generates a SIGINT in most common operating systems.

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![eliteforce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@eliteforce](https://www.ruby-forum.com/u/eliteforce)\
**Post date:** [July 17, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/catch-c-signals-blocks/98668/6 "2006-07-17T13:00:03Z")

</div>

On 7/17/06, Elite F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So my last question left open is, if there’s a difference between do end  
> and { } blocks

I think there’s no difference in behaviour. There is a difference in  
their precedence. {} blocks have higher precedence although usually  
that doesn’t make a difference. I just use {} when the block fits in a  
single line with a single statement and do…end for everything else.

Pedro.
