capitalizing words

I am trying to write a function that converts something like "It's a dog's life" to "It's A Dog's Life". I am doing this with string.gsub(/ \b\w/) {$&.upcase} but this gives me "It'S A Dog'S Life" - how can I tell the regexp to ignore apostrophes when looking for a new word to capitalize?

item.capitalize ??

--Scott T.

http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Handlers/UTF8Handler.html#M000289

raghus wrote:

I am trying to write a function that converts something like "It's a dog's life" to "It's A Dog's Life". I am doing this with string.gsub(/ \b\w/) {$&.upcase} but this gives me "It'S A Dog'S Life" - how can I tell the regexp to ignore apostrophes when looking for a new word to capitalize?

string.titleize will give you the same result you're getting. Doesn't help you at all with this problem, but, just for future reference.

But, back to the problem you're having. This seems to do the trick.

string.gsub(/(^|[^\w'])([\w'])/) {$&.upcase}

Jon - ok that worked. Thank you. Thanks for the titleize tip as well. Scott - mystr.capitalize will capitalize just the first character - so I'd end up with "It's a dog's life" rather than "It's A Dog's Life"