Feature #15919
closedOffset parameter for `Integer#times`
Description
I request an optional argument on Integer#times
to set the offset at start, just like Enumerator#with_index
.
5.times(3){|i| p i}
# >> 3
# >> 4
# >> 5
# >> 6
# >> 7
I think there are plenty of use cases, especially when the offset is 1.
Updated by phluid61 (Matthew Kerwin) almost 5 years ago
Could be confusing. "5 times 3" is 15.
Updated by ana06 (Ana Maria Martinez Gomez) almost 5 years ago
I like the idea. It could also be a Hash and appart from offset
, we could have step
as well:
5.times(offset: 3, step: 2) {|i| p i}
# >> 3
# >> 5
# >> 7
# >> 9
# >> 11
Updated by shevegen (Robert A. Heiler) almost 5 years ago
It took me a moment to understand the proposal (I am not sure why I am having a hard time understanding
proposals lately; but in my defence, I am getting older, and it is currently very hot in central europe -
hope you folks have a better time with the weather).
So to the proposal - actually I sort of agree with sawa that there may be use cases for it. Or at the
least I can imagine that this may be the case.
I also think that Matthew's comment makes sense; people can misread "5 times 3" as "5 x 3" (I deliberately
omitted the () and . there.
How about some hash-parameter instead? Like:
5.times(start_at: 3){|i| p i}
Or some other parameter, perhaps just start: or something like that.
Although I should also point out that this would be different to what sawa suggested, so since
it is his proposal, the focus should be on his initial suggestion primarily, not the deviation;
I was just trying to integrate what Matthew commented on.
I am fine either way and I am fine with the current behaviour, too - I really am neutral mostly
in regards to this issue. :)
Updated by ibylich (Ilya Bylich) almost 5 years ago
Numeric#step
does something similar:
> 3.step(to: 10, by: 2) { |a| p a }
3
5
7
9
Updated by duerst (Martin Dürst) almost 5 years ago
I agree with all the commenters that using keyword arguments is crucial (and I prefer start_at:
(or maybe just start:
) to offset:
). I also agree that Integer#step
seems already good enough. Maybe we could add a line about Integer#step
in the documentation of Integer#times
.
Updated by ko1 (Koichi Sasada) over 4 years ago
- Assignee set to matz (Yukihiro Matsumoto)
It seems difficult to introduce (Integer#step
)...
Matz, what do you think about it?
Updated by matz (Yukihiro Matsumoto) over 4 years ago
I think Numeric#step
was OK. Is there any reason to stick with Integer#times
?
Matz.
Updated by matz (Yukihiro Matsumoto) over 4 years ago
- Status changed from Open to Rejected