Created
April 27, 2024 21:28
-
-
Save romiras/6729bb83ce876b3f76e1c1e42d6659fb to your computer and use it in GitHub Desktop.
Break a loop gracefully on Ctrl-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signal.trap("INT") { throw :exit_loop } | |
def aaa | |
catch :exit_loop do | |
loop do | |
puts 'foo' | |
p Benchmark.realtime { sleep 1 } | |
puts 'bar' | |
end | |
end | |
Signal.trap('INT', 'DEFAULT') | |
puts "Loop exited!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment