"capitalize" also letters after "-"?

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.

On Sat, May 10, 2008 at 10:52 PM, Iñaki Baz C. [email protected] wrote:

But String#capitalize method just capitalized first letter:

Thanks a lot.


Iñaki Baz C.

Is this too slow?

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

Harry

El Sábado, 10 de Mayo de 2008, Harry K.
escribió:> >

p str.split(/-/).map {|x| x.capitalize}.join("-")
Hummm:

Benchmark.realtime { “accept”.capitalize }
=> 2.21729278564453e-05

Benchmark.realtime { “accept”.split(’-’).map {|w|
w.capitalize}.join(’-’) }
=> 3.69548797607422e-05

On Sat, 10 May 2008, Iñaki Baz C. wrote:

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?

I have to express healthy skepticism as to whether performance is so
critical that you can’t just do:

str.gsub!(/((^|-).)/) { $1.upcase }

and if it is, then I’ll express healthy skepticism as to whether or
not Ruby is the right language for the job :slight_smile:

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).

David

El Sábado, 10 de Mayo de 2008, David A. Black escribió:

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|
w.capitalize}.join(’-’) }
=> 3.09944152832031e-05

Benchmark.realtime { “accept-asdasd-qweqwe”.gsub!(/((^|-).)/) {
$1.upcase } }
=> 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 :slight_smile:

Good response :slight_smile:

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 :wink:

Thanks a lot.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs