summaryrefslogtreecommitdiff
path: root/lib/timeout.rb
diff options
context:
space:
mode:
authorCosmicOppai <cosmicoppai@protonmail.com>2024-10-18 01:08:38 +0530
committergit <svn-admin@ruby-lang.org>2024-12-03 06:49:55 +0000
commitb156efc2a6f2574eb7eebbf34b0027e19a0c5838 (patch)
tree3b2cb608e877f53ccfd6552ab15b63c91bfa466a /lib/timeout.rb
parente7dd185e2147ff0ce5ad6a3de563749a4ad8ccb2 (diff)
[ruby/timeout] updated doc and kept the nil compatiability
https://github.com/ruby/timeout/commit/f992632cf3
Diffstat (limited to 'lib/timeout.rb')
-rw-r--r--lib/timeout.rb9
1 files changed, 5 insertions, 4 deletions
diff --git a/lib/timeout.rb b/lib/timeout.rb
index 56d31db61a..ae3ac8c5ae 100644
--- a/lib/timeout.rb
+++ b/lib/timeout.rb
@@ -141,9 +141,10 @@ module Timeout
# Perform an operation in a block, raising an error if it takes longer than
# +sec+ seconds to complete.
#
- # +sec+:: Number of seconds to wait for the block to terminate. Any number
- # may be used, including Floats to specify fractional seconds. A
+ # +sec+:: Number of seconds to wait for the block to terminate. Any non-negative number
+ # or nil may be used, including Floats to specify fractional seconds. A
# value of 0 or +nil+ will execute the block without any timeout.
+ # Any negative value will raise the ArgumentError
# +klass+:: Exception Class to raise if the block fails to terminate
# in +sec+ seconds. Omitting will use the default, Timeout::Error
# +message+:: Error message to raise with Exception Class.
@@ -164,8 +165,8 @@ module Timeout
# Timeout</tt> into your classes so they have a #timeout method, as well as
# a module method, so you can call it directly as Timeout.timeout().
def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
- raise ArgumentError, "Timeout sec must be a positive number" unless sec.is_a?(Numeric) && sec >= 0
- return yield(sec) if sec.zero?
+ raise ArgumentError, "Timeout sec must be a non-negative number" if sec && !(sec.is_a?(Numeric) && sec >= 0)
+ return yield(sec) if sec == nil or sec.zero?
message ||= "execution expired"