Project

General

Profile

Actions

Bug #15867

closed

Enumerable#minmax inconsistent behavior with Time ranges

Added by arzezak (Ariel Rzezak) almost 5 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin17]
[ruby-core:92789]

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!


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #15807: Range#minmax is slow and never returns for endless rangesClosedActions
Actions #1

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

  • Related to Bug #15807: Range#minmax is slow and never returns for endless ranges added
Actions #2

Updated by jeremyevans (Jeremy Evans) over 4 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.

Fixes [Bug #15867]
Fixes [Bug #15807]

Actions

Also available in: Atom PDF

Like0
Like0Like0