[ruby-core:90940] [Ruby trunk Bug#15518] good old Infinite range notation behavior

From: sawadatsuyoshi@...
Date: 2019-01-09 07:19:52 UTC
List: ruby-core #90940
Issue #15518 has been updated by sawa (Tsuyoshi Sawada).


With finite ranges, the class of the elements in the return value seems to reflect the class of the `step` parameter as shown below (although, I am not sure why the one with rational has `1` instead of `(1/1)`. A bug, perhaps?):

```ruby
(1..10).step(1).first(5) # => [1, 2, 3, 4, 5]
(1..10).step(1.0).first(5) # => [1.0, 2.0, 3.0, 4.0, 5.0]
(1..10).step(1/1r).first(5) #=> [1, (2/1), (3/1), (4/1), (5/1)]
```

Given that, without explicit `step`, the returned numbers are integers, it should be understood that the default step for a range is the integer `1`:

```ruby
(1..10).first(5) # => [1, 2, 3, 4, 5]
```

The above claim that the class of the number is determined by the `step` parameter (and nothing else, such as the end of range) is confirmed by the fact that a range ending with `Float::INFINITY` that has default step `1` returns integers.

```ruby
(1..Float::INFINITY).first(5) #=> [1, 2, 3, 4, 5]
```

Given the argument above, I think `(1..Float::INFINITY).step(1).first(5)` should return integers as follows:

```ruby
(1..Float::INFINITY).step(1).first(5) # => [1, 2, 3, 4, 5]
```

Thus, to me, neither Ruby 2.5.3 nor Ruby 2.6.0's behavior makes sense.

----------------------------------------
Bug #15518: good old Infinite range notation behavior
https://bugs.ruby-lang.org/issues/15518#change-76140

* Author: sakuro (Sakuro OZAWA)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin18]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
Ruby 2.5.3's behavior

~~~
# without step, it produces integer sequence
(1..Float::INFINITY).first(10) #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# with step, it produces floats instead of integers
(1..Float::INFINITY).step(1).first(10) #=> [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
~~~

Ruby 2.6.0's behavior

~~~
# endless range
(1..).first(10) #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# with step, all numbers are integer now
(1..).step(1).first(10) #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# old idiom with Float::INFINITY
(1..Float::INFINITY).first(10) #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(1..Float::INFINITY).step(1).first(10) #=> FloatDomainError (Infinity)
~~~

Which are intended change and which are not?




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

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

In This Thread

Prev Next