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

···

--
Iñaki Baz Castillo

Is this too slow?

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

Harry

···

On Sat, May 10, 2008 at 10:52 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

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ñaki Baz Castillo

--
A Look into Japanese Ruby List in English

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

···

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

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?

--
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS June 9-12 Berlin
   ADVANCING WITH RAILS June 16-19 Berlin
   INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

Hummm:

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

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

···

El Sábado, 10 de Mayo de 2008, Harry Kakueki escribió:

On Sat, May 10, 2008 at 10:52 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
> 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ñaki Baz Castillo

Is this too slow?

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

--
Iñaki Baz Castillo

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.

···

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

--
Iñaki Baz Castillo