Project

General

Profile

Actions

Feature #19094

closed

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

Added by ioquatix (Samuel Williams) about 2 years ago. Updated almost 2 years 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 #1

Updated by jeremyevans0 (Jeremy Evans) about 2 years ago

  • Tracker changed from Bug to Feature
  • Backport deleted (2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN)
Actions #2

Updated by ioquatix (Samuel Williams) about 2 years ago

  • Tracker changed from Feature to Bug
  • Description updated (diff)
  • Backport set to 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
Actions #3

Updated by ioquatix (Samuel Williams) about 2 years ago

  • Tracker changed from Bug to Feature
  • Backport deleted (2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN)

Updated by sawa (Tsuyoshi Sawada) about 2 years ago

I don't think it is hard to implement. All you need is a twinkle little star.

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

  def sleep
    Kernel::sleep(*@time)
  end
end

Updated by ioquatix (Samuel Williams) about 2 years ago

Haha, great, but I think it's odd you can't just write sleep(nil). I guess Ruby uses Qundef internally, but user can't write sleep(undef), but sleep(*nil) is close. Actually, I didn't even know you can write sleep(*nil) to get that behaviour, so I think it's not a good solution in general, it's too obscure.

Updated by ioquatix (Samuel Williams) about 2 years ago

By the way, my motivation was to make this easier for the user: https://github.com/socketry/async/issues/161

Updated by Eregon (Benoit Daloze) about 2 years ago

Mutex#sleep accepts an explicit nil argument and it's documented.
+1, this inconsistency is rather weird (and rather messy to implement in TruffleRuby).

I don't know about false though, that's can't convert FalseClass into time interval (TypeError) for both sleep methods which seems fair.

Updated by matz (Yukihiro Matsumoto) almost 2 years ago

I understand the usefulness of the proposal. Accepted.
But do not propose other changes just for consistency with this change.

Matz.

Updated by ioquatix (Samuel Williams) almost 2 years ago

  • Status changed from Open to Closed

Merged.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like1Like0Like0Like0Like0Like0Like0