Feature #15919

Offset parameter for `Integer#times`

Added by sawa (Tsuyoshi Sawada) over 1 year ago. Updated over 1 year ago.

Target version:


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) over 1 year ago

Could be confusing. "5 times 3" is 15.

Updated by ana06 (Ana Maria Martinez Gomez) over 1 year 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) over 1 year 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) over 1 year ago

Numeric#step does something similar:

> 3.step(to: 10, by: 2) { |a| p a }

Updated by duerst (Martin Dürst) over 1 year 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 1 year 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 1 year ago

I think Numeric#step was OK. Is there any reason to stick with Integer#times?



Updated by matz (Yukihiro Matsumoto) over 1 year ago

  • Status changed from Open to Rejected

Also available in: Atom PDF