* [ruby-core:100488] [Ruby master Feature#17279] Allow a negative step in Range#step with a block
@ 2020-10-22 2:23 muraken
0 siblings, 0 replies; only message in thread
From: muraken @ 2020-10-22 2:23 UTC (permalink / raw)
To: ruby-core
Issue #17279 has been reported by mrkn (Kenta Murata).
----------------------------------------
Feature #17279: Allow a negative step in Range#step with a block
https://bugs.ruby-lang.org/issues/17279
* Author: mrkn (Kenta Murata)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
`Range#step` prohibits a negative step when a block is given.
```
>> (6..3).step(-1) {|i| p i }
Traceback (most recent call last):
5: from /home/mrkn/.rbenv/versions/2.7/bin/irb:23:in `<main>'
4: from /home/mrkn/.rbenv/versions/2.7/bin/irb:23:in `load'
3: from /home/mrkn/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/irb-1.2.4/exe/irb:11:in `<top (required)>'
2: from (irb):1
1: from (irb):1:in `step'
ArgumentError (step can't be negative)
```
But `Range#step` allows a negative step when it is called without a block. In this case, `Range#step` creates an ArithmeticSequence, and `ArithmeticSequence#each` can iterate with a negative step.
```
>> (6..3).step(-1).each {|i| p i }
6
5
4
3
=> ((6..3).step(-1))
```
I think the prohibition of a negative step in `Range#step` has already been meaningless, so it may be better to permit it for consistency.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2020-10-22 2:23 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-22 2:23 [ruby-core:100488] [Ruby master Feature#17279] Allow a negative step in Range#step with a block muraken
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).