[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Decending Range

Dominic Marks

1/30/2007 11:30:00 AM

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

2 Answers

come

1/30/2007 12:50:00 PM

0


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

dblack

1/30/2007 1:47:00 PM

0