Hi --

On Sat, 7 Nov 2009, Marnen Laibow-Koser wrote:

> Michael W. Ryder wrote:
> [...]
>> .  I much prefer the
>> simplicity of Basic and C with for loops that can go either direction.
>
> That's because you're trying to write C in Ruby.  There are far more
> idiomatic ways of doing things -- and they *are* clearer, at least in a
> Ruby context.
>
>> 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.
>
> That's great in a language like C that doesn't have very good string
> handling.  The Ruby way to do this would be
> city, state, zip = string.split(/\s+/)

You'd need to take multi-word city names into account, though. So
maybe:

   city, state, zip = /\A(.*)\s+(\S+)\s+(\S+)\Z/.match(str).captures


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)