Issue #15807 has been updated by janosch-x (Janosch M=FCller).


mame (Yusuke Endoh) wrote:
> `Range#max` is not consistent with `Enumerable#minmax`.

Thanks for pointing this out, I wasn't aware of that. Floats are another ex=
ample:
``` ruby
(1..(5.5)).max # =3D> 5.5
(1..(5.5)).minmax # =3D> [1, 5]
```

Maybe we could fix / speedup `minmax` only for ranges where `begin` and `en=
d` are `NIL_P`/`is_integer_p`/`Float::INFINITY`, and call super for all oth=
er cases?

A check for `Float::INFINITY` might be helpful here, too, while we're at it=
: https://github.com/ruby/ruby/blob/ae6c195f30f76b1dc4a32a0a91d35fe80f6f85d=
3/range.c#L808

My use case has to do with Regexp quantification. I can go into more detail=
, but to describe it quickly, I want to provide information about how many =
chars a Regexp can match in https://github.com/ammar/regexp_parser. Ranges,=
 some ending with Infinity, are the most natural choice for this, but minma=
x would be useful in the related code. Also, I don't want to hand out "dang=
erous" Ranges to gem users. Maybe I will `#extend` the Ranges with a safe m=
inmax.


----------------------------------------
Bug #15807: Range#minmax is slow and never returns for endless ranges
https://bugs.ruby-lang.org/issues/15807#change-77812

* Author: janosch-x (Janosch M=FCller)
* Status: Open
* Priority: Normal
* Assignee: =

* Target version: =

* ruby -v: 2.6.3p62
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
current situation:

- `(1..).minmax` runs forever
- `(1..).max` raises "cannot get the maximum of endless range"
- `(1..Float::INFINITY).minmax` runs forever
- `(1..Float::INFINITY).max` returns instantly
- `(1..1_000_000_000).minmax` takes one minute
- `(1..1_000_000_000).max` returns instantly

my suggestion:

- implement `minmax` in range.c, return [`range_min`, `range_max`]
- for endless ranges, this will trigger the same error as `max` does
- delegate to enum (rb_call_super) only if called with a block (?)

i could perhaps provide a PR if you can point me to some information on how=
 to contribute.

cheers!



-- =

https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>