-
-
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: | |
# | |
old_handler = Signal.trap('SIGNALNAME') {} | |
Signal.trap('SIGNALNAME') do | |
# Whatever you'd like to do. | |
# Then: | |
old_handler.call | |
end |
Thanks Clifford! You're right.
Interesting, I did not know that Signal.trap returns the previously defined handler.
… 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 ;) )
@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.
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.
Absolutely.
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.