# Decending Range

**URL:** <https://www.ruby-forum.com/t/decending-range/83878>\
**Category:** Ruby\
**Created:** [January 30, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/decending-range/83878 "2007-01-30T12:30:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![djwmarks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@djwmarks](https://www.ruby-forum.com/u/djwmarks)\
**Post date:** [January 30, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/decending-range/83878/1 "2007-01-30T12:30:54Z")

</div>

Hello,

I was surprised of to find this behaviour:

irb(main):008:0\> a, b = 1, 100  
=\> [1, 100]  
irb(main):009:0\> (a…b).to\_a.length  
=\> 100  
irb(main):010:0\> (b…a).to\_a.length  
=\> 0

Now I am aware that only a simple .reverse is required to work  
around this but this seemed a little un-ruby like. Is there  
some thoughtful reason to not have ranges in both  
directions that I’m missing?

Cheers,  
Dominic

---

<div class="post-metadata">

**Author:** ![djwmarks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@djwmarks](https://www.ruby-forum.com/u/djwmarks)\
**Post date:** [January 30, 2007, 1:54pm UTC](https://www.ruby-forum.com/t/decending-range/83878/2 "2007-01-30T13:54:20Z")

</div>

On 1/30/07, Dominic M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> 0
> 
> Now I am aware that only a simple .reverse is required to work  
> around this but this seemed a little un-ruby like. Is there  
> some thoughtful reason to not have ranges in both  
> directions that I’m missing?
> 
> Cheers,  
> Dominic

Hrm.

irb(main):001:0\> a,b = 1,100  
=\> [1, 100]  
irb(main):002:0\> ar = Array.new  
=\> []  
irb(main):003:0\> ar1 = Array.new  
=\> []  
irb(main):004:0\> ar = (b…a).to\_a  
=\> []  
irb(main):005:0\> ar1 = (100…1).to\_a  
=\> []  
irb(main):006:0\> b.downto(a) { |x| ar1.push(x) }  
=\> 100  
irb(main):007:0\> p ar1  
[100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84,  
83,  
82, 81  
, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64,  
63,  
62, 61  
, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44,  
43,  
42, 41  
, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24,  
23,  
22, 21  
, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

I thought that ranges worked the way you did, until I looked it up:  
[http://www.ruby-doc.org/core/classes/Range.html](http://www.ruby-doc.org/core/classes/Range.html)

Ranges can be constructed using objects of any type, as long as the  
objects  
can be compared using their \<=\> operator and they support the succ  
method to  
return the next object in sequence.  
So, it looks like 100…1 won’t work because

irb(main):001:0\> 100.succ  
=\> 101

---

<div class="post-metadata">

**Author:** ![djwmarks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@djwmarks](https://www.ruby-forum.com/u/djwmarks)\
**Post date:** [January 30, 2007, 1:56pm UTC](https://www.ruby-forum.com/t/decending-range/83878/3 "2007-01-30T13:56:21Z")

</div>

Hi,

That’s normal since backward ranges cannot be iterated. They have no  
size. Just boundaries, so you can still use them for other purposes,  
though. For instance when one side of the range should reference a  
string from the end : string=hello string[0…-1] =\> “hello”.

If (0…-1) would have been {0, -1}, then string[0…-1] =\> “ho”. Not  
very usefull. Maybe the reason is just because backward ranges are  
more usefull in the first way.

Come.

---

<div class="post-metadata">

**Author:** ![djwmarks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@djwmarks](https://www.ruby-forum.com/u/djwmarks)\
**Post date:** [January 30, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/decending-range/83878/4 "2007-01-30T19:05:31Z")

</div>

Hi –

On Tue, 30 Jan 2007, Jason M. wrote:

> Ranges can be constructed using objects of any type, as long as the objects  
> can be compared using their \<=\> operator and they support the succ method to  
> return the next object in sequence.

That’s actually not a necessary condition – you can have a range of  
floats too. A range is fundamentally a kind of boolean test filter  
for inclusion, and only secondarily a sort of ersatz array. I tend to  
think too much is sometimes expected of ranges in the latter role.

David

---

<div class="post-metadata">

**Author:** ![djwmarks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@djwmarks](https://www.ruby-forum.com/u/djwmarks)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/decending-range/83878/5 "2007-09-25T23:04:46Z")

</div>

On 1/30/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That’s actually not a necessary condition – you can have a range of  
> floats too. A range is fundamentally a kind of boolean test filter  
> for inclusion, and only secondarily a sort of ersatz array. I tend to  
> think too much is sometimes expected of ranges in the latter role.

Oh. Maybe someone should update the content at  
[http://www.ruby-doc.org/core/classes/Range.html](http://www.ruby-doc.org/core/classes/Range.html)
