Hi --

On Sat, 7 Nov 2009, Michael W. Ryder wrote:

> Marnen Laibow-Koser wrote:
>> Michael W. Ryder wrote:
>> [...]
>>> But if you wanted to do something like:
>>>    i = 10;
>>>    while (i > 0)
>>>    {
>>>      printf("%d/n", i--);
>>>    }
>>> in Ruby you would have to do something like:
>>>    i = 10
>>>    while (i > 0)
>>>      puts i
>>>      i -= 1
>>>    end
>> 
>> No.
>> 
>> 10.downto(1) do |i|
>>   puts i
>> end
>> 
>>> As far as I can tell there is no way in Ruby to use .each or .times to
>>> go backwards.
>> 
>> That's what .downto is for.  (Have you ever needed this?  I have not.)
>> 
>
> I missed the downto method, I guess that is a problem when you have so many 
> different ways to do the same basic things.  I much prefer the simplicity of 
> Basic and C with for loops that can go either direction. As far as going 
> backwards I use it a lot to parse strings of the form "city name ST 
> 12345-6789" to City, State, and Zip Code fields.  I look for the first blank 
> from the end of the string and assume everything after it is the Zip Code, I 
> then find the next two non-blank characters and assign them to State, and 
> everything else is the City name.

I think one thing that's getting lost in the sauce here is that Ruby
does have idioms like:

   a -= 1

for bumping things up and down and other operations. So you can
maintain a manual index on a collection or string traversal easily if
you need to. I'd say that most of the time, though, you won't need to.


David

-- 
The          Ruby training with D. Black, G. Brown, J.McAnally
Compleat     Jan 22-23, 2010, Tampa, FL
Rubyist      http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)