Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define #sleep in more detail #893

Merged
merged 1 commit into from
Feb 3, 2022
Merged

Define #sleep in more detail #893

merged 1 commit into from
Feb 3, 2022

Conversation

ksss
Copy link
Collaborator

@ksss ksss commented Feb 2, 2022

Defined Kernel#sleep in more detail.

Fix some problems

1. Complex inherits from Numeric, but does not accept.

$ ruby -e 'sleep 1.to_c'
-e:1:in `sleep': can't convert Complex into time interval (TypeError)
	from -e:1:in `<main>'

2. Any object that does not inherit from Numeric can be passed as an argument, as long as #divmod is defined.

https://github.com/ruby/spec/blob/1a68f6e0ffede9f0c2c6eb8c6dff75924df79d91/core/kernel/sleep_spec.rb#L25-L29

More detail

Since #divmod is defined for both Integer and Float, it could be just (_Divmod duration) -> Integer, but there are some surprises in the documentation, and I used (Integer | Float | _Divmod) in reference to CRuby implementation.

CRuby implementation is time_timespec function in time.c.

https://github.com/ruby/ruby/blob/af107710457b4bd148c6e5ee7a336ed4961f5423/time.c#L2560-L2609

There is a method File.utime that uses time_timespec function, but its behavior is not described in ruby/spec, and I've narrowed down the scope to sleep.

Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@soutaro soutaro merged commit d935887 into ruby:master Feb 3, 2022
@ksss ksss deleted the sleep branch February 3, 2022 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants