Project

General

Profile

Actions

Feature #19094

closed

`sleep(nil)` vs `sleep()` and replicating the default implementation.

Added by ioquatix (Samuel Williams) over 1 year ago. Updated about 1 year ago.

Status:
Closed
Target version:
-
[ruby-core:110554]

Description

> sleep(nil)
(irb):1:in `sleep': can't convert NilClass into time interval (TypeError)

However, I feel that this makes implementing a compatible sleep method a little difficult.

def sleep(time = nil)
  if time
    sleep_some(time)
  else
    sleep_forever
  end
end

Can we consider allowing sleep(nil) and sleep(false) to be the same as sleep() to simplify this behaviour?

Otherwise, it's hard to proxy (e.g. fiber scheduler, or even just a plain old variable argument).

e.g.

class Sleeper
  def initialize(time = nil)
    @time = time
  end

  def sleep
    Kernel::sleep(@time) # Hard to get the behaviour of `sleep()` here without explicitly handling/checking.
  end
end
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like1Like0Like0Like0Like0Like0Like0