Skip to content

Instantly share code, notes, and snippets.

@romiras
Created April 27, 2024 21:28
Show Gist options
  • Save romiras/6729bb83ce876b3f76e1c1e42d6659fb to your computer and use it in GitHub Desktop.
Save romiras/6729bb83ce876b3f76e1c1e42d6659fb to your computer and use it in GitHub Desktop.
Break a loop gracefully on Ctrl-C
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