[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"capitalize" also letters after "-" ?

Iñaki Baz Castillo

5/10/2008 1:52:00 PM

Hi, I receive strings like:

content-language
accept-resource-priority

and I want to "capitalize" them in this way:

Content-Language
Accept-Resource-Priority

But String#capitalize method just capitalized first letter:

Content-language
Accept-resource-priority


Is there any "fast" method for what I want?
Yes, I could implement it extending String class or my own class < String, =
but=20
performance is very important and I'd prefer using a method written in C=20
(as "String#capitalized"). Does is exist?


Thanks a lot.


=2D-=20
I=C3=B1aki Baz Castillo

3 Answers

Harry Kakueki

5/10/2008 2:34:00 PM

0

On Sat, May 10, 2008 at 10:52 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrot=
e:
> Hi, I receive strings like:
>
> content-language
> accept-resource-priority
>
> and I want to "capitalize" them in this way:
>
> Content-Language
> Accept-Resource-Priority
>
> But String#capitalize method just capitalized first letter:
>
> Content-language
> Accept-resource-priority
>
>
> Is there any "fast" method for what I want?
> Yes, I could implement it extending String class or my own class < String=
, but
> performance is very important and I'd prefer using a method written in C
> (as "String#capitalized"). Does is exist?
>
>
> Thanks a lot.
>
>
> --
> I=F1aki Baz Castillo
>
>
Is this too slow?

str =3D "accept-resource-priority"
p str.split(/-/).map {|x| x.capitalize}.join("-")

Harry

--=20
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Iñaki Baz Castillo

5/10/2008 3:02:00 PM

0

El S=E1bado, 10 de Mayo de 2008, Harry Kakueki escribi=F3:
> On Sat, May 10, 2008 at 10:52 PM, I=F1aki Baz Castillo <ibc@aliax.net> wr=
ote:
> > Hi, I receive strings like:
> >
> > content-language
> > accept-resource-priority
> >
> > and I want to "capitalize" them in this way:
> >
> > Content-Language
> > Accept-Resource-Priority
> >
> > But String#capitalize method just capitalized first letter:
> >
> > Content-language
> > Accept-resource-priority
> >
> >
> > Is there any "fast" method for what I want?
> > Yes, I could implement it extending String class or my own class <
> > String, but performance is very important and I'd prefer using a method
> > written in C (as "String#capitalized"). Does is exist?
> >
> >
> > Thanks a lot.
> >
> >
> > --
> > I=F1aki Baz Castillo
>
> Is this too slow?
>
> str =3D "accept-resource-priority"
> p str.split(/-/).map {|x| x.capitalize}.join("-")

Hummm:

Benchmark.realtime { "accept".capitalize }
=3D> 2.21729278564453e-05

Benchmark.realtime { "accept".split('-').map {|w|=20
w.capitalize}.join('-') }
=3D> 3.69548797607422e-05





=2D-=20
I=F1aki Baz Castillo

Iñaki Baz Castillo

5/10/2008 3:49:00 PM

0

El S=C3=A1bado, 10 de Mayo de 2008, David A. Black escribi=C3=B3:
> I have to express healthy skepticism as to whether performance is *so*
> critical that you can't just do:
>
> str.gsub!(/((^|-).)/) { $1.upcase }

Thanks, but it seems slower than using "split"/"join":

Benchmark.realtime { "accept-asdasd-qweqwe".split('-').map {|w|=20
w.capitalize}.join('-') }
=3D> 3.09944152832031e-05

Benchmark.realtime { "accept-asdasd-qweqwe".gsub!(/((^|-).)/) {=20
$1.upcase } }
=3D> 3.60012054443359e-05


> and if it is, then I'll express healthy skepticism as to whether or
> not Ruby is the right language for the job :-)

Good response :)


> I don't know of any C library that does the above, so if you need it,
> you should probably go ahead and write it. You can base it off
> rb_capitalize(_bang).

ok, I'll keep this as an alternative ;)

Thanks a lot.



=2D-=20
I=C3=B1aki Baz Castillo