Skip to content

Instantly share code, notes, and snippets.

@floere
Created July 11, 2011 00:52
Show Gist options
  • Save floere/1075153 to your computer and use it in GitHub Desktop.
Save floere/1075153 to your computer and use it in GitHub Desktop.
When trapping Signals in your lib, please think about doing it like so:
# When trapping Signals in your lib, please think about doing it like so:
#
old_handler = Signal.trap('SIGNALNAME') {}
Signal.trap('SIGNALNAME') do
# Whatever you'd like to do.
# Then:
old_handler.call
end
@cjheath
Copy link

cjheath commented Jul 11, 2011

Actually, it's even simpler than that.

old_handler = Signal.trap('SIGNALNAME') do

... do shit...

old_handler.call
end

This works because the handler is called asynchronously, after the assignment.

@floere
Copy link
Author

floere commented Jul 11, 2011

Thanks Clifford! You're right.

@kronn
Copy link

kronn commented Jul 11, 2011

Interesting, I did not know that Signal.trap returns the previously defined handler.

@floere
Copy link
Author

floere commented Jul 11, 2011

… and now you know :)
(Had you been reading my blog, you'd have known it in February http://florianhanke.com/blog/2011/02/20/searching-with-picky-live-reloading-indexes.html#reloading ;) )

@floere
Copy link
Author

floere commented Jul 12, 2011

@cjheath Actually, the moment it is called is not that important. It's firstly about local variable declaration and bindings, only secondly about the moment of the call.

@cjheath
Copy link

cjheath commented Jul 12, 2011

True dat. The point is that Signal#trap doesn't yield, it just sets a handler and returns the old one immediately, so the assignment to old_handler happens straight away, before anything else can (except perhaps in another thread). The handler can only ever get called after the assignment.

@floere
Copy link
Author

floere commented Jul 12, 2011

Absolutely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment