"capitalize" also letters after "-" ?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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
 
H

Harry Kakueki

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.
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/list.html
 
I

Iñaki Baz Castillo

El S=E1bado, 10 de Mayo de 2008, Harry Kakueki escribi=F3:
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

Iñaki Baz Castillo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top