⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 13 - adding a timeout to a long-running operation.rb

📁 O Reilly Ruby Cookbook source code
💻 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 + -