From: Jason Mayer Date: 2007-01-30T21:53:34+09:00 Subject: Re: Decending Range ------=_Part_124289_18969042.1170161611977 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 1/30/07, Dominic Marks 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 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 ------=_Part_124289_18969042.1170161611977--