# Decending Range

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

On 1/30/07, Dominic M. [email protected] 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

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

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.

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

On 1/30/07, [email protected] [email protected] 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