From: William James Date: 2005-10-08T09:21:49+09:00 Subject: Re: Code for title-casing (US) snail addresses? rpardee@gmail.com wrote: > And thanks again! > > If I can beg your indulgence a bit further... I'm having trouble > elaborating this so it does the right thing for street names like > 'McGrath'. The simpler version is: > > def prettify(address) > abbreviations = %w(PO NE NW SE SW) > prefixes2 = %w(Mc O' D') > prefixes3 = %w(Mac) > address.gsub!(/(\d)([A-Z]+)/) { $1 + $2.downcase } > address.gsub(/[A-Z]+/) do |w| > # abbreviations.include?(w) ? w : w.capitalize > if abbreviations.include?(w) then > w > else > w.capitalize > end > end > end > > Which works admirably, except it gives back "Mcgrath", rather than > "McGrath". > > I *thought* I'd be able to tack a second Array.include? on there, like > so: > > def prettify(address) > abbreviations = %w(PO NE NW SE SW) > prefixes2 = %w(Mc O' D') > prefixes3 = %w(Mac) > address.gsub!(/(\d)([A-Z]+)/) { $1 + $2.downcase } > address.gsub(/[A-Z]+/) do |w| > # abbreviations.include?(w) ? w : w.capitalize > if abbreviations.include?(w) then > w > else > w.capitalize > end > if prefixes2.include?(w[0..1]) then > w[2..2] = w[2..2].upcase > end > end > end > > But when I do that, calls like prettify("20 E MCGRAW ST") just return > "20" > > I've read over the gsub docs, but don't see why the above does not work > as expected. Anybody got a clue for me? > > Thanks! > > -Roy def prettify(address) abbreviations = %w(PO NE NW SE SW) prefixes = %w(Mc O' D' Mac) address.gsub(/\S+/) do |w| case when abbreviations.include?(w) w when w =~ /^\d/ w.downcase else w = w.capitalize prefixes.inject(w){|wrd,pre| wrd[pre.size,1]=wrd[pre.size,1].upcase if wrd.index(pre)==0 wrd } end end end