End ruby ​​script on infinite loops

I want the ruby ​​script to end on infinite loops, or if the ruby ​​script takes more than 10 seconds than the completion of the script itself. I know, using the ctrl key or any keyboard interrupt, I can do this, but I want the script to automatically terminate itself if it takes more than 10 seconds

+3
source share
1 answer
require "timeout"
Timeout.timeout(10) do
  loop do
    ...
  end
end
+6
source

All Articles