Skip to content

Commit f992632

Browse files
committed
updated doc and kept the nil compatiability
1 parent ffc8d7c commit f992632

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lib/timeout.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ def self.ensure_timeout_thread_created
141141
# Perform an operation in a block, raising an error if it takes longer than
142142
# +sec+ seconds to complete.
143143
#
144-
# +sec+:: Number of seconds to wait for the block to terminate. Any number
145-
# may be used, including Floats to specify fractional seconds. A
144+
# +sec+:: Number of seconds to wait for the block to terminate. Any non-negative number
145+
# or nil may be used, including Floats to specify fractional seconds. A
146146
# value of 0 or +nil+ will execute the block without any timeout.
147+
# Any negative value will raise the ArgumentError
147148
# +klass+:: Exception Class to raise if the block fails to terminate
148149
# in +sec+ seconds. Omitting will use the default, Timeout::Error
149150
# +message+:: Error message to raise with Exception Class.
@@ -164,8 +165,8 @@ def self.ensure_timeout_thread_created
164165
# Timeout</tt> into your classes so they have a #timeout method, as well as
165166
# a module method, so you can call it directly as Timeout.timeout().
166167
def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
167-
raise ArgumentError, "Timeout sec must be a positive number" unless sec.is_a?(Numeric) && sec >= 0
168-
return yield(sec) if sec.zero?
168+
raise ArgumentError, "Timeout sec must be a non-negative number" if sec && !(sec.is_a?(Numeric) && sec >= 0)
169+
return yield(sec) if sec == nil or sec.zero?
169170

170171
message ||= "execution expired"
171172

0 commit comments

Comments
 (0)