def titleize(string)
string.split(\" \").map {|word| word.capitalize}.join(\" \")
end
This titleizes every single word, but how do I capture cert
The answer of @codenamev is not quite doing the job:
EXCLUSIONS = %w(a the and or to)
"and the answer is all good".titleize(exclude: EXCLUSIONS)
# => "And the Answer Is all Good"
^^^
Exclusions should match trailing word boundaries. Here's an improved version:
# encoding: utf-8
class String
def titleize(options = {})
exclusions = options[:exclude]
return ActiveSupport::Inflector.titleize(self) unless exclusions.present?
self.underscore.humanize.gsub(/\b(['’`]?(?!(#{exclusions.join('|')})\b)[a-z])/) { $&.capitalize }
end
end
"and the answer is all good".titleize(exclude: EXCLUSIONS)
# => "And the Answer Is All Good"
^^^