Trans
4/2/2009 6:13:00 PM
On Apr 2, 7:55=A0am, I=F1aki Baz Castillo <i...@aliax.net> 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:
>
> =A0 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 =3D =A0"Record-ROUTE"
> hname.split("-").map {|w| w.capitalize }.join("-") } =A0=3D> =A0"Record-R=
oute"
> ------------
>
> 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:
>
> =A0 'a' + 1 =3D> 'b'
> =A0 'a' + =BFX? =3D> 'A'
>
> but in Ruby#String I just find String#next which just increment the
> char in one unit ("a".next =3D> "b").
>
> Any suggestion? Thanks a lot.
>
> Thanks a lot.
I would look at the camelcase implementations of Facets, English or
ActiveSupport. They are pretty close to what you want. You may even be
able to use one of them outright.
T.