📄 13 - adding a timeout to a long-running operation.rb
字号:
# This code will sleep forever... OR WILL IT?require 'timeout'before = Time.nowbegin status = Timeout.timeout(5) { sleep }rescue Timeout::Error puts "I only slept for #{Time.now-before} seconds."end# I only slept for 5.035492 seconds.#---def count_for_five_seconds $counter = 0 begin Timeout::timeout(5) { loop { $counter += 1 } } rescue Timeout::Error puts "I can count to #{$counter} in 5 seconds." endendcount_for_five_seconds# I can count to 2532825 in 5 seconds.$counter # => 2532825#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -