Ruby: Titleize: How do I ignore smaller words like 'and', 'the', 'or, etc

前端 未结 9 1804
猫巷女王i
猫巷女王i 2020-12-31 13:13
def titleize(string)
  string.split(\" \").map {|word| word.capitalize}.join(\" \")
end

This titleizes every single word, but how do I capture cert

9条回答
  •  耶瑟儿~
    2020-12-31 13:50

    If you want not to capitalize and or the, just do the following:

    def titleize(string)
      nocaps = "and"
      string.split(" ").map { |word| nocaps.include?(word) ? word : word.capitalize }.join(" ")
    end
    

提交回复
热议问题