Catch ^c/signals? blocks?

E

Elite Force

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.
 
O

Ola Bini

Elite said:
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.

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

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

"Yields falsehood when quined" yields falsehood when quined.
 
E

Elite Force

Ola said:
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
 
P

Pedro Côrte-Real

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top