Rails' titleize
method removes hyphens, and Ruby's capitalize
method does not capitalize the word that comes after a hyphen. I want something like the following:
"mary-joe spencer-moore" => "Mary-Joe Spencer-Moore"
"mary-louise o'donnell" => "Mary-Louise O'Donnell"
Check Titelize implementation and from it you can get:
"mary-joe spencer-moore".humanize.gsub(/\b('?[a-z])/) { $1.capitalize }
will give you => "Mary-Joe Spencer-Moore"
and you can write a function for it in string class, Add to intalizers:
class String
def my_titleize
humanize.gsub(/\b('?[a-z])/) { $1.capitalize }
end
end
and then from your code:
"mary-joe spencer-moore".my_titleize
You could also get the desired result by splitting up your string and titleizing the sections separately:
"mary-louise o'donnell".split('-').map(&:titleize).join('-')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With