Actions
Bug #15867
closedEnumerable#minmax inconsistent behavior with Time ranges
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin17]
Description
With Date
the behavior is consistent for inclusive and exclusive ranges:
>> (Date.new(2019, 7, 1)..Date.new(2019, 7, 3)).min
=> #<Date: 2019-07-01 ((2458666j,0s,0n),+0s,2299161j)>
>> (Date.new(2019, 7, 1)..Date.new(2019, 7, 3)).max
=> #<Date: 2019-07-03 ((2458668j,0s,0n),+0s,2299161j)>
>> (Date.new(2019, 7, 1)..Date.new(2019, 7, 3)).minmax
=> [#<Date: 2019-07-01 ((2458666j,0s,0n),+0s,2299161j)>, #<Date: 2019-07-03 ((2458668j,0s,0n),+0s,2299161j)>]
>> (Date.new(2019, 7, 1)...Date.new(2019, 7, 3)).minmax
=> [#<Date: 2019-07-01 ((2458666j,0s,0n),+0s,2299161j)>, #<Date: 2019-07-02 ((2458667j,0s,0n),+0s,2299161j)>]
With Time
and inclusive ranges min
and max
are returned.
>> (Time.new(2019, 7, 1, 10)..Time.new(2019, 7, 1, 12)).min
=> 2019-07-01 10:00:00 -0300
>> (Time.new(2019, 7, 1, 10)..Time.new(2019, 7, 1, 12)).max
=> 2019-07-01 12:00:00 -0300
But minmax
raises a TypeError
:
Traceback (most recent call last):
6: from /Users/ariel/.rbenv/versions/2.6.2/bin/irb:23:in `<main>'
5: from /Users/ariel/.rbenv/versions/2.6.2/bin/irb:23:in `load'
4: from /Users/ariel/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
3: from (irb):8
2: from (irb):8:in `minmax'
1: from (irb):8:in `each'
TypeError (can't iterate from Time)
For exclusive ranges min
is returned but max
raises.
>> (Time.new(2019, 7, 1, 10)...Time.new(2019, 7, 1, 12)).min
=> 2019-07-01 10:00:00 -0300
>> (Time.new(2019, 7, 1, 10)...Time.new(2019, 7, 1, 12)).max
Traceback (most recent call last):
7: from /Users/ariel/.rbenv/versions/2.6.2/bin/irb:23:in `<main>'
6: from /Users/ariel/.rbenv/versions/2.6.2/bin/irb:23:in `load'
5: from /Users/ariel/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
4: from (irb):11
3: from (irb):11:in `max'
2: from (irb):11:in `max'
1: from (irb):11:in `each'
TypeError (can't iterate from Time)
If min
and max
are returned for an inclusive Time
range, shouldn't minmax
be returned as well?
Thanks!
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
- Related to Bug #15807: Range#minmax is slow and never returns for endless ranges added
Updated by jeremyevans (Jeremy Evans) over 5 years ago
- Status changed from Open to Closed
Applied in changeset git|d5c60214c45bafc1cf2a516f852394986f9c84bb.
Implement Range#minmax
Range#minmax was previous not implemented, so calling #minmax on
range was actually calling Enumerable#minmax. This is a simple
implementation of #minmax by just calling range_min and range_max.
Actions
Like0
Like0Like0