I have a program that will trap Ctrl + c, but it can be trapped just once. When you type exit to exit from the irb session, the signal generated by Ctrl + c can’t be trapped again. Any ideas? This is the program:
require 'irb'
#trap "INT" do
# IRB.start
#end
Signal.trap("INT") { IRB.start }
count = 0
loop do
count += 1
puts count
puts "Value = #{@value}" if defined? @value
sleep 1
end
The problem is that IRB sets its own handler for sigint when you invoke it, which overrides your handler. To fix this, you can reset your handler again, after IRB is finished, like this:
This will cause a bunch of warning when you invoke IRB the second time though, but that’s a general problem with invoking IRB multiple times.