A better capitalize method

I’m just creating an app that has a registration form and I want to ensure that people’s names have capital letters in the right places.

String#capitalize just capitalizes the first letter of a string so it’s not exactly what I want.


"laura ann".capitalize
=> "Laura ann"
"o'shea".capitalize
=> "O'shea"

At a glance I couldn’t see any other ruby method or ActiveSupport method that does what I want here so a solution can be reached using String#gsub.


"laura ann".gsub(/\b([\S])/){|m| m.upcase}
=> "Laura Ann"

"o'shea".gsub(/\b([\S])/){|m| m.upcase}
=> "O'Shea"