Project

General

Profile

Feature #15609

Kernel#sleep returns Float instead of Integer

Added by znz (Kazuhiro NISHIYAMA) 6 months ago. Updated 5 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:91582]

Description

rb_f_sleep uses time() internally, so it always returns Integer even if it called with Float.

https://github.com/ruby/ruby/blob/124957566f041e5c6e8f678dfaf62cae381826dc/process.c#L4840-L4857

I think returning Float is more useful than Integer.
(But I have no real use cases now.)


Files

ruby-changes.patch (2.53 KB) ruby-changes.patch gabewild37 (Gabe Wild), 08/13/2019 04:26 PM

History

Updated by gabewild37 (Gabe Wild) 5 days ago

Hi, I don't know if anyone still wants this feature, but, since I wanted to get started contributing to ruby, I have implemented the following changes in my patch. Should an integer be entered into the function, the function acts like it used to. Ex: sleep(1) ==> 1, sleep(2) ==> 2. However, should a float be entered instead, the function will return a float. Ex: sleep(1.0) ==> 1.001077, sleep(2.2) ==> 2.202255. I did this by using gettimeofday() instead of time(), so the float returned is accurate to the microsecond. I also added some test code, and updated the documentation I found above the function in process.c to reflect my new changes. However, I think, instead of doing what I did, maybe making a new function or adding an optional flag could be better. Now that the return values are no longer rounded to integers, the changes in my patch could cause some backwards compatibility issues if someone is relying on rb_f_sleep to return an integer. Since I am new to both ruby and open source in general, any comments, suggestions, or improvements are appreciated.

Also available in: Atom PDF