ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:109793] [Ruby master Feature#18986] Raises Exception for Range#last(n) with Float::INFINITY
@ 2022-08-30 19:51 masasakano (Masa Sakano)
  0 siblings, 0 replies; only message in thread
From: masasakano (Masa Sakano) @ 2022-08-30 19:51 UTC (permalink / raw)
  To: ruby-core

Issue #18986 has been reported by masasakano (Masa Sakano).

----------------------------------------
Feature #18986: Raises Exception for Range#last(n) with Float::INFINITY
https://bugs.ruby-lang.org/issues/18986

* Author: masasakano (Masa Sakano)
* Status: Open
* Priority: Normal
----------------------------------------
When a (Numeric) argument is given to `Range#last(n)` for a Range with `Float::INFINITY`, it runs into an infinite loop (see a small code snippet below).
There is no use case for the feature as far as I can think of, and it is hard to detect.

```ruby
(6..Float::INFINITY).last(1)  # => infinite loop!!
(6..).last(1)   # cannot get the last element of endless range (RangeError)
(-Float::INFINITY..4).last(1) # can't iterate from Float (TypeError)
(..4).last(1)                 # can't iterate from Float (TypeError)
```

Let me suggest raising an Exception (maybe RangeError, in the same way as an endless Range) when `last(n)` is specified for a Range whose end value is `Float::INFINITY`.  
I note `(6..Float::INFINITY).to_a` has the same issue.



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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-08-30 19:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-30 19:51 [ruby-core:109793] [Ruby master Feature#18986] Raises Exception for Range#last(n) with Float::INFINITY masasakano (Masa Sakano)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).