Why does the titlecase
mess up the name? I have:
John Mark McMillan
and it turns it into:
>> \"john mark McM
You may also encounter names with two capital letters, such as McLaren, McDonald etc.
Have not spent time trying to improve it, but you could always do
Code
# Rails.root/config/initializers/string.rb
class String
def titleize_name
self.split(" ")
.collect{|word| word[0] = word[0].upcase; word}
.join(" ").gsub(/\b('?[a-z])/) { $1.capitalize }
end
end
Examples
[2] pry(main)> "test name".titleize_name
=> "Test Name"
[3] pry(main)> "test name-name".titleize_name
=> "Test Name-Name"
[4] pry(main)> "test McName-name".titleize_name
=> "Test McName-Name"