Rudolf Polzer
9/9/2003 9:07:00 PM
Scripsit illa aut ille »Karsten Meier« <discussion@internetlabor.de>:
> Thank you for the feedback.
> (Are only germans interested in this kind of stuff?)
No idea.
> I think most usages in perl just don''t make sense in ruby,
> normally it is better to use a local variable, you also have no trouble
> to put file handles in a variable in ruby.
Neither do you in current Perl versions:
open my $fh, "</some/file" or die ''hard'';
[...]
> The perl cookbook mentions also it is possible to use it for temporary
> signal handlers, like this (perl code)
> local $SIG(INT) = ''IGNORE'';
> I never needed to install a signal handler at all, so I''m not sure
> if this something intersting, and if it apply to ruby.
In Ruby one would just use exceptions for this purpose. Hm...
nearly, since there is no "ON ERROR RESUME NEXT".
Seems like it''s impossible to use a block for both the signal
handler and the code which is to be executed.
Hm... wouldn''t something like this (but also supporting Proc#yield
and Proc#[] which is trivial to add) be a nice extension?
class Proc
alias :origCall :call
def trap(signal, &handler)
(@traps ||= []) << [signal, handler]
self
end
def do_trapping(&block)
savedtraps = []
(@traps || []).each() do |t|
savedtraps << [t[0], Kernel.trap(t[0], t[1])]
end
begin
block.origCall() # do NOT call yield() here (endless recursion)!
ensure
savedtraps.each() do |t|
Kernel.trap(t[0], t[1])
end
end
end
def call(*args)
do_trapping do
origCall(*args)
end
end
end
trap(''INT'') do
puts "Aiiieee!!!1"
end
proc do
puts "AAA"
sleep 5
puts "BBB"
end.trap(''INT'') do
puts "SIGINT caught"
end.trap(''QUIT'') do
puts "SIGQUIT caught"
end.call() # <-- this MUST NOT be left out
sleep 5