Steven D'Aprano wrote:
> Howdy,
>   

Hi
> I'm not a Ruby developer *at all*, I use Python, but this is not flame-
> bait. I'm interested in how Ruby folks find using intervals.
>
> In Python, we deal with integer ranges virtually exclusively with the 
> range() function. range() always results in a half-open interval:
>
> range(5) => 0, 1, 2, 3, 4
> range(1, 5) => 1, 2, 3, 4
> range(4, -1, -1) => 4, 3, 2, 1, 0
>
> The start argument is always included, the end argument is never 
> included, and there is an optional step size (defaults to 1).
>
>
> I understand that in Ruby you have quite a few choices, some of which are 
> half-open like Python, some of which are closed:
>
> 0..5 => 0, 1, 2, 3, 4, 5
> 0...5 => 0, 1, 2, 3, 4
>   
I nearly always use 0..5
Sometimes 0...5 in special cases.
> 5.downto(1) => 5, 4, 3, 2, 1
> 1.upto(5) => 1, 2, 3, 4, 5
> 5.times() => 0, 1, 2, 3, 4
> 5.step(11, 3) => 5, 8, 11
>   

Rarely do I use these. And even then it's only 5.times do ... end

> and the Range.new method.
>   

Never.

> My question is: how useful are all these different mechanisms? Do you 
> find that having two operators .. and ... is a blessing, or a curse 
>   
> because you can never remember which is which?
>   

Maybe it's just me, but I nearly always use .. so it's easy to remember 
that ... is the "special" case for me.

> How useful are the closed interval forms? Do you find yourself making off-
> by-one errors or needing to increment/decrement variables by one?
>
> e.g. do you often need to write things like:
>
> start.step(end + 1, increment){| i | block }
> start.step(end - 1, increment){| i | block }
>   

Never.

> Writing in Python, I almost never need to "shift the fence-posts", so to 
> speak. E.g. I virtually never need to write something like:
>
> range(start, end+1)
>
> to avoid an off-by-one error. When I used to program in Pascal (which 
> exclusively uses closed intervals) I used to need to do it all the time. 
> What's the Ruby experience?
>
>
>
> Thank you,
>   

Maybe I'm unusual (most of my code is for my own pet projects) but 
nearly all my iteration is done with arrays and hashes or objects that 
act like them, so my most commonly used loop construct is 'each'. After 
that it's probably 'until', then 'while', and 'times' last.

That's been my experience.

-Justin