Merged
Conversation
JRuby is currently converting Time objects to floats when generating a random value from a range: ``` $ RBENV_VERSION=jruby-9.2.7.0 ruby -e 'puts rand(Time.now..Time.now)' 1562034267.423605 ``` MRI returns a Time object: ``` $ RBENV_VERSION=2.6.3 ruby -e 'puts rand(Time.now..Time.now)' 2019-07-01 19:27:25 -0700 ``` I believe the `RubyNumeric` check I added is the same as what MRI uses in `rb_check_to_float`: https://github.com/ruby/ruby/blob/0b858425e1e4f2de40dc0d8e5dd105a2fd93e478/object.c#L3769
Member
|
specs look good but if you look at the build the proposed fix introduced regressions: https://api.travis-ci.org/v3/job/553080882/log.txt ... the 'proper' fix likely won't involve a change a |
This looks to be the behavior that MRI expects for float-like values in ranges. MRI calls `range.end - range.begin` when generating a random value from a range: https://github.com/ruby/ruby/blob/5d9e91afe08c470485333f6c6e034d05ea3ee908/random.c#L1051 If the return value responds to `to_int` it is treated as an integer, so `CustomRangeInteger` works fine: https://github.com/ruby/ruby/blob/5d9e91afe08c470485333f6c6e034d05ea3ee908/object.c#L3243 Float-like objects that respond to `to_f` are treated differently and must be numeric: https://github.com/ruby/ruby/blob/5d9e91afe08c470485333f6c6e034d05ea3ee908/object.c#L3769
Contributor
Author
|
@kares I think the fix is correct ( |
eregon
pushed a commit
to ruby/spec
that referenced
this pull request
Jul 27, 2019
* Support Time ranges in rand JRuby is currently converting Time objects to floats when generating a random value from a range: ``` $ RBENV_VERSION=jruby-9.2.7.0 ruby -e 'puts rand(Time.now..Time.now)' 1562034267.423605 ``` MRI returns a Time object: ``` $ RBENV_VERSION=2.6.3 ruby -e 'puts rand(Time.now..Time.now)' 2019-07-01 19:27:25 -0700 ``` I believe the `RubyNumeric` check I added is the same as what MRI uses in `rb_check_to_float`: https://github.com/ruby/ruby/blob/0b858425e1e4f2de40dc0d8e5dd105a2fd93e478/object.c#L3769 * Return float from `CustomRangeFloat#-` This looks to be the behavior that MRI expects for float-like values in ranges. MRI calls `range.end - range.begin` when generating a random value from a range: https://github.com/ruby/ruby/blob/5d9e91afe08c470485333f6c6e034d05ea3ee908/random.c#L1051 If the return value responds to `to_int` it is treated as an integer, so `CustomRangeInteger` works fine: https://github.com/ruby/ruby/blob/5d9e91afe08c470485333f6c6e034d05ea3ee908/object.c#L3243 Float-like objects that respond to `to_f` are treated differently and must be numeric: https://github.com/ruby/ruby/blob/5d9e91afe08c470485333f6c6e034d05ea3ee908/object.c#L3769
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JRuby is currently converting Time objects to floats when generating a
random value from a range:
MRI returns a Time object:
I believe the
RubyNumericcheck I added is the same as what MRI usesin
rb_check_to_float: https://github.com/ruby/ruby/blob/0b858425e1e4f2de40dc0d8e5dd105a2fd93e478/object.c#L3769