From: "t3ch.dude" Date: 2009-11-11T05:00:09+09:00 Subject: Re: Ruby doesn't implement x++ for Fixnum's because ??? On Nov 7, 4:39 am, "David A. Black" wrote: > 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 > >> "cityname ST 12345-6789" toCity,State, andZipCode fields.  I look > >> for the first blank from the end of the string and assume everything > >> after it is theZipCode, I then find the next two non-blank characters > >> and assign them toState, and everything else is theCityname. > > > 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+/) irb(main):004:0> str = "Washington Court House OH 43160" => "Washington Court House OH 43160" irb(main):005:0> zip, state, city = str.reverse.split(/ /,3).map {|e| e.reverse} => ["43160", "OH", "Washington Court House"] > > You'd need to take multi-wordcitynames into account, though. So > maybe: > >    city,state,zip= /\A(.*)\s+(\S+)\s+(\S+)\Z/.match(str).captures > > David >