# Decending Range

**URL:** https://rubytalk.org/t/decending-range/34934
**Category:** ruby-talk
**Created:** [30 January 2007 11:29 UTC](https://rubytalk.org/t/decending-range/34934 "2007-01-30T11:29:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Dominic\_Marks](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@Dominic\_Marks](https://rubytalk.org/u/Dominic_Marks)
#### Post date: [30 January 2007 11:29 UTC](https://rubytalk.org/t/decending-range/34934/1 "2007-01-30T11:29:45Z")

</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: ![Jason\_Mayer](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jason\_Mayer](https://rubytalk.org/u/Jason_Mayer)
#### Post date: [30 January 2007 12:53 UTC](https://rubytalk.org/t/decending-range/34934/2 "2007-01-30T12:53:34Z")

</div>

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

> **···**
>
> On 1/30/07, Dominic Marks \<dom@helenmarks.co.uk\> wrote:
> 
> > 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: ![come](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@come](https://rubytalk.org/u/come)
#### Post date: [30 January 2007 12:55 UTC](https://rubytalk.org/t/decending-range/34934/3 "2007-01-30T12:55:06Z")

</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.

> **···**
>
> On 30 jan, 12:29, Dominic Marks \<d...@helenmarks.co.uk\> wrote:
> 
> > 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: ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)
#### Post date: [30 January 2007 13:47 UTC](https://rubytalk.org/t/decending-range/34934/4 "2007-01-30T13:47:12Z")

</div>

Hi --

> **···**
>
> On Tue, 30 Jan 2007, Jason Mayer 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
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

### Author: ![Jason\_Mayer](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jason\_Mayer](https://rubytalk.org/u/Jason_Mayer)
#### Post date: [30 January 2007 14:06 UTC](https://rubytalk.org/t/decending-range/34934/5 "2007-01-30T14:06:11Z")

</div>

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)

> **···**
>
> On 1/30/07, dblack@wobblini.net \<dblack@wobblini.net\> wrote:
> 
> > Hi --
> > 
> > On Tue, 30 Jan 2007, Jason Mayer 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.
