From: Andrew Timberlake Date: 2009-04-02T21:00:05+09:00 Subject: Re: String#capitalize more complex On Thu, Apr 2, 2009 at 1:55 PM, Iñaki Baz Castillo wrote: > Hi, I receive headers (the protocol syntax allows them being lowcase, > upcase or a mix). For example, the following header names are > equivalent: > > a) Record-Route > b) record-route > c) RECORD-ROUTE > d) Record-ROUTE > > I'm trying to do a method to convert all of them to: > >  Record-Route > > > Basically what I need is to capitalize all the header name parts after > splitting it using "-" as separator. I can do it as follows: > > ------------ > hname =  "Record-ROUTE" > hname.split("-").map {|w| w.capitalize }.join("-") }  =>  "Record-Route" > ------------ > > Benchmark.realtime { hname.split("-").map {|w| w.capitalize }.join("-") } > 1.69277191162109e-05 > > > Is there any option even faster? I think it would be faster if I > operate directly in the original string instead of doing "split" and > "join" (since these last methods create more strings so allocate > memory for them and so). I would prefer to do the modification "in > place". > > BTW, could I know how to convert "a" char into "A"? I expected > something as in c: > >  'a' + 1 => 'b' >  'a' + ¿X? => 'A' > > but in Ruby#String I just find String#next which just increment the > char in one unit ("a".next => "b"). > > Any suggestion? Thanks a lot. > > Thanks a lot. > > > -- > Iñaki Baz Castillo > > > Have a look at Net::HTTPHeader and it's code as all of this is handled in there http://www.ruby-doc.org/core/classes/Net/HTTPHeader.html Andrew Timberlake http://ramblingsonrails.com http://www.linkedin.com/in/andrewtimberlake "I have never let my schooling interfere with my education" - Mark Twain