# Range weirdness

**URL:** <https://www.ruby-forum.com/t/range-weirdness/70494>\
**Category:** Ruby\
**Created:** [September 29, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/range-weirdness/70494 "2006-09-29T18:01:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [September 29, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/range-weirdness/70494/1 "2006-09-29T18:01:08Z")

</div>

This surprised me:

C:\Documents and Settings\Alex\>ruby -e “(4…1).each{|a| puts a}”

C:\Documents and Settings\Alex\>ruby -e “(1…4).each{|a| puts a}”  
1  
2  
3  
4

C:\Documents and Settings\Alex\>

Should it have done? I find it somewhat odd that ranges can only be  
enumerated in one direction. Is there a reason for it?

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [September 29, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/range-weirdness/70494/2 "2006-09-29T18:15:11Z")

</div>

On 9/29/06, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Should it have done? I find it somewhat odd that ranges can only be  
> enumerated in one direction. Is there a reason for it?

There are many reason for it.

First, you need ranges with no elements.

Example:

> (0 … x.size-1).each{|i| puts x[i]}

Now it works correctly with x=[]. It wouldn’t if (0…-1) was 0.

Another example:

> x = [“a”, “b”, “c”, “d”]  
> p x[1…-2]  
> [“b”, “c”]

It wouldn’t work either if (1…-2) meant [1,0,-1,-2].
