Project

General

Profile

Feature #15919

Offset parameter for `Integer#times`

Added by sawa (Tsuyoshi Sawada) 2 months ago. Updated 21 days ago.

Status:
Rejected
Priority:
Normal
Target version:
-
[ruby-core:93100]

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.

History

Updated by phluid61 (Matthew Kerwin) 2 months ago

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

Updated by ana06 (Ana Maria Martinez Gomez) 2 months 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) 2 months 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) 2 months 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) 2 months 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) 21 days 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) 21 days ago

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

Matz.

#8

Updated by matz (Yukihiro Matsumoto) 21 days ago

  • Status changed from Open to Rejected

Also available in: Atom PDF